Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Fix Rust Agent Laminar usage telemetry

Magnus Müllerba7df96250c4
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 62f4ec210..e81e48421 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -1442,6 +1442,14 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- The Rust wrapper stores the new public settings and forwards screenshot/clickable-element limits into `MessageManager`, preserving the existing top-level `browser_use.Agent` Python implementation while keeping `from browser_use.rust import Agent` as the Rust-core interface.
- Proof: browser-use `.venv/bin/python -m pytest -q tests/ci/test_rust_agent.py -k 'rust_agent_constructor or rust_agent_runtime_signatures or rust_agent_constructor_type_hints or rust_agent_run_type_hints or rust_agent_action_model_helper_type_hints or rust_agent_browser_state_helper_type_hints or rust_agent_llm_message_helper_type_hints or rust_agent_unannotated_helper_type_hints or rust_terminal_usage_prices_anthropic_raw_cache_reads or rust_terminal_usage_prices_token_count_events'` and browser-use `.venv/bin/python -m py_compile browser_use/rust/service.py`.
+276. Rust Agent Laminar and cache-write usage clarity
+ - Rust Laminar LLM replay now passes the model-visible message array directly as the `LLM` span input and the assistant message array as span output, avoiding the extra `{model, provider, messages}` wrapper that made Laminar render nested input strangely.
+ - Terminal request observability now emits the full model-visible request without the old message window or text budget, and keeps inline media data in `llm_input`; the Python replay converts Rust `media` parts into OpenAI-style `image_url` data URLs so Laminar can render screenshots inline while secret-like fields remain redacted.
+ - Model, provider, tool inventory, previews, and usage details now move to scalar span attributes, including indexed `gen_ai.prompt.N.*` / `gen_ai.completion.N.*` attributes expected by Laminar/OpenTelemetry LLM spans.
+ - Rust usage summaries now expose `total_prompt_cache_creation_tokens` and `total_prompt_cache_creation_cost`, and token-count reconstruction sums per-call cache-write tokens even when cumulative `total_token_usage` omits the named cache-write field while keeping it in `total_tokens`.
+ - Direct Python-interface diagnostic through `from browser_use.rust import Agent` with Browser Use cloud completed a 3-step `https://example.com` task successfully and proved the cost breakdown: `$0.11142345` total, including `$0.09388125` for 25,035 cache-write tokens and `$0.0110772` for 36,924 cache-read tokens.
+ - Proof: terminal `cargo test -p browser-use-agent turn_request_event_carries_full_llm_input_messages -- --nocapture`, terminal `cargo test -p browser-use-agent turn_request_event_carries_all_observability_messages_without_text_budget -- --nocapture`, terminal `cargo fmt --check`, browser-use `.venv/bin/python -m pytest -q tests/ci/test_rust_agent.py -k 'rust_terminal_usage_prices_token_count_events or rust_terminal_usage_prices_anthropic_raw_cache_reads or rust_terminal_usage_sums_token_count_cache_creation or rust_token_summary_does_not_double_count_cache_reads or rust_agent_laminar_run_summary_populates_current_span'`, browser-use `.venv/bin/python -m py_compile browser_use/rust/service.py browser_use/tokens/service.py browser_use/tokens/views.py`, and the saved diagnostic conversation at `/tmp/rust_usage_diag_conv`.
+
## Current Verification
- terminal `cargo test -p browser-use-agent driver_passes_populated_per_call_request_to_open_stream -- --nocapture`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 1a78b6a41..3fd987bfb 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -2363,11 +2363,13 @@ def _usage_total_tokens(
def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary:
input_tokens = 0
cached_input_tokens = 0
+ cache_creation_tokens = 0
completion_tokens = 0
total_tokens = 0
cost = 0.0
invocations = 0
token_count_invocations = 0
+ token_count_cache_creation_tokens = 0
for event in events:
event_type = _event_type(event)
@@ -2378,6 +2380,7 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary
event_completion_tokens = _usage_completion_tokens(usage)
input_tokens += event_input_tokens
cached_input_tokens += event_cached_input_tokens
+ cache_creation_tokens += event_cache_creation_tokens
completion_tokens += event_completion_tokens
total_tokens += _usage_total_tokens(
usage, event_input_tokens, event_cache_creation_tokens, event_completion_tokens
@@ -2389,7 +2392,14 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary
token_usage = _token_count_usage(payload)
if token_usage is None:
continue
- input_tokens, cached_input_tokens, cache_creation_tokens = _input_usage_buckets(token_usage)
+ input_tokens, cached_input_tokens, total_cache_creation_tokens = _input_usage_buckets(token_usage)
+ last_usage = _token_count_last_usage(payload)
+ if isinstance(last_usage, dict):
+ _, _, last_cache_creation_tokens = _input_usage_buckets(last_usage)
+ token_count_cache_creation_tokens += last_cache_creation_tokens
+ elif total_cache_creation_tokens:
+ token_count_cache_creation_tokens = total_cache_creation_tokens
+ cache_creation_tokens = token_count_cache_creation_tokens
completion_tokens = _usage_completion_tokens(token_usage)
total_tokens = _usage_total_tokens(token_usage, input_tokens, cache_creation_tokens, completion_tokens)
token_count_invocations += 1
@@ -2410,6 +2420,8 @@ def _usage_from_events(events: list[dict[str, Any]], model: str) -> UsageSummary
total_prompt_cost=0.0,
total_prompt_cached_tokens=cached_input_tokens,
total_prompt_cached_cost=0.0,
+ total_prompt_cache_creation_tokens=cache_creation_tokens,
+ total_prompt_cache_creation_cost=0.0,
total_completion_tokens=completion_tokens,
total_completion_cost=0.0,
total_tokens=total_tokens,
@@ -2432,6 +2444,7 @@ async def _usage_from_events_with_costs(
total_prompt_cost = 0.0
total_completion_cost = 0.0
total_prompt_cached_cost = 0.0
+ total_prompt_cache_creation_cost = 0.0
total_cost = 0.0
priced_invocations = 0
@@ -2455,6 +2468,7 @@ async def _usage_from_events_with_costs(
total_prompt_cost += cost.prompt_cost
total_completion_cost += cost.completion_cost
total_prompt_cached_cost += cost.prompt_read_cached_cost or 0.0
+ total_prompt_cache_creation_cost += cost.prompt_cache_creation_cost or 0.0
total_cost += cost.total_cost
if priced_invocations == 0:
@@ -2469,6 +2483,7 @@ async def _usage_from_events_with_costs(
update={
'total_prompt_cost': total_prompt_cost,
'total_prompt_cached_cost': total_prompt_cached_cost,
+ 'total_prompt_cache_creation_cost': total_prompt_cache_creation_cost,
'total_completion_cost': total_completion_cost,
'total_cost': total_cost,
'by_model': model_stats,
@@ -2593,27 +2608,140 @@ def _terminal_laminar_turn_output(events: list[dict[str, Any]], raw_usage: dict[
}
+def _terminal_laminar_system_messages(span_input: dict[str, Any]) -> list[dict[str, Any]]:
+ system_messages = []
+ system_parts = span_input.get('system')
+ if not isinstance(system_parts, list):
+ return system_messages
+ for part in system_parts:
+ if isinstance(part, dict):
+ text = part.get('text') or part.get('content')
+ else:
+ text = part
+ if isinstance(text, str) and text:
+ system_messages.append({'role': 'system', 'content': [{'type': 'text', 'text': text}]})
+ return system_messages
+
+
+def _terminal_laminar_content_part_for_span(part: Any) -> Any:
+ if not isinstance(part, dict):
+ return part
+ part_type = part.get('type')
+ if part_type == 'media':
+ mime_type = part.get('mime_type')
+ mime_type = mime_type if isinstance(mime_type, str) and mime_type else 'application/octet-stream'
+ url = part.get('url')
+ data = part.get('data')
+ resolved_url = None
+ if isinstance(url, str) and url:
+ resolved_url = url
+ elif isinstance(data, str) and data:
+ resolved_url = f'data:{mime_type};base64,{data}'
+ if resolved_url and mime_type.startswith('image/'):
+ image_url: dict[str, Any] = {'url': resolved_url}
+ detail = part.get('detail')
+ if isinstance(detail, str) and detail:
+ image_url['detail'] = detail
+ return {'type': 'image_url', 'image_url': image_url}
+ if resolved_url:
+ return {'type': 'file', 'file_data': resolved_url}
+ content = part.get('content')
+ if isinstance(content, list):
+ normalized = dict(part)
+ normalized['content'] = [_terminal_laminar_content_part_for_span(item) for item in content]
+ return normalized
+ return part
+
+
+def _terminal_laminar_message_for_span(message: Any) -> Any:
+ if not isinstance(message, dict):
+ return message
+ content = message.get('content')
+ if not isinstance(content, list):
+ return message
+ normalized = dict(message)
+ normalized['content'] = [_terminal_laminar_content_part_for_span(part) for part in content]
+ return normalized
+
+
+def _terminal_laminar_span_input_messages(span_input: dict[str, Any]) -> list[dict[str, Any]]:
+ messages = span_input.get('messages')
+ if not isinstance(messages, list):
+ messages = []
+ normalized_messages = [_terminal_laminar_message_for_span(message) for message in messages]
+ return [*_terminal_laminar_system_messages(span_input), *normalized_messages]
+
+
+def _terminal_laminar_span_output_messages(span_output: dict[str, Any]) -> list[dict[str, Any]]:
+ messages = span_output.get('messages')
+ return messages if isinstance(messages, list) else []
+
+
+def _terminal_laminar_message_text(message: dict[str, Any]) -> str:
+ content = message.get('content')
+ if isinstance(content, str):
+ return content
+ if not isinstance(content, list):
+ return _laminar_preview(content, limit=4000) if content else ''
+ parts = []
+ for part in content:
+ if isinstance(part, dict):
+ text = part.get('text') or part.get('content')
+ if isinstance(text, str):
+ parts.append(text)
+ elif part.get('type') in {'image', 'image_url', 'media', 'input_image'}:
+ parts.append('[image]')
+ elif part:
+ parts.append(str(_laminar_preview(part, limit=1000)))
+ elif isinstance(part, str):
+ parts.append(part)
+ return '\n'.join(parts)
+
+
+def _terminal_laminar_indexed_message_attributes(
+ messages: list[dict[str, Any]], prefix: str, *, max_messages: int = 20
+) -> dict[str, Any]:
+ attributes: dict[str, Any] = {}
+ for index, message in enumerate(messages[:max_messages]):
+ if not isinstance(message, dict):
+ continue
+ role = message.get('role')
+ if isinstance(role, str):
+ attributes[f'{prefix}.{index}.role'] = role
+ content = _terminal_laminar_message_text(message)
+ if content:
+ attributes[f'{prefix}.{index}.content'] = _laminar_preview(content, limit=12_000)
+ if len(messages) > max_messages:
+ attributes[f'{prefix}.truncated_count'] = len(messages) - max_messages
+ return attributes
+
+
def _terminal_laminar_gen_ai_attributes(
span_input: dict[str, Any],
span_output: dict[str, Any],
usage: dict[str, int | float] | None,
) -> dict[str, Any]:
+ input_messages = _terminal_laminar_span_input_messages(span_input)
+ output_messages = _terminal_laminar_span_output_messages(span_output)
attributes: dict[str, Any] = {
'gen_ai.operation.name': 'chat',
'gen_ai.request.model': str(span_input.get('model') or ''),
'gen_ai.system': str(span_input.get('provider') or ''),
- 'gen_ai.input.messages': _laminar_preview(span_input.get('messages') or [], limit=60_000),
'gen_ai.system_instructions': _laminar_preview(span_input.get('system') or [], limit=30_000),
- 'gen_ai.output.messages': _laminar_preview(span_output.get('messages') or [], limit=40_000),
}
+ attributes.update(_terminal_laminar_indexed_message_attributes(input_messages, 'gen_ai.prompt'))
+ attributes.update(_terminal_laminar_indexed_message_attributes(output_messages, 'gen_ai.completion'))
if usage:
attributes.update(
{
'gen_ai.usage.input_tokens': usage.get('input_tokens'),
'gen_ai.usage.output_tokens': usage.get('output_tokens'),
'gen_ai.usage.total_tokens': usage.get('total_tokens'),
+ 'llm.usage.total_tokens': usage.get('total_tokens'),
}
)
+ if usage.get('cost_usd'):
+ attributes['gen_ai.usage.cost'] = usage.get('cost_usd')
return attributes
@@ -2643,8 +2771,10 @@ def _record_laminar_terminal_llm_spans(
usage = _terminal_laminar_usage_summary(raw_usage)
start_time = _event_time_seconds(turn_events[0], 0.0)
end_time = _event_time_seconds(turn_events[-1], start_time)
- with _laminar_start_span('rust_core.llm', input=span_input, span_type='LLM'):
+ span_input_messages = _terminal_laminar_span_input_messages(span_input)
+ with _laminar_start_span('rust_core.llm', input=span_input_messages, span_type='LLM'):
span_output = _terminal_laminar_turn_output(turn_events, raw_usage)
+ span_output_messages = _terminal_laminar_span_output_messages(span_output)
_laminar_set_span_attributes(
{
'runtime': 'browser_use.rust',
@@ -2653,15 +2783,27 @@ def _record_laminar_terminal_llm_spans(
'turn_index': span_index,
'turn_idx': _int_value(span_input.get('turn_idx')),
'attempt': _int_value(span_input.get('attempt')),
+ 'system_prompt_tokens': _int_value(span_input.get('system_prompt_tokens')),
+ 'tools_count': _int_value(span_input.get('tools_count')),
+ 'tool_names': _laminar_preview(span_input.get('tool_names') or [], limit=2000),
+ 'message_count': _int_value(span_input.get('message_count')),
+ 'omitted_earlier_messages': _int_value(span_input.get('omitted_earlier_messages')),
+ 'truncated': bool(span_input.get('truncated')),
+ 'assistant_output_preview': span_output.get('assistant_output_preview') or '',
+ 'thinking_preview': span_output.get('thinking_preview') or '',
+ 'tool_calls_count': _int_value(span_output.get('tool_calls_count')),
+ 'tool_call_names': _laminar_preview(span_output.get('tool_call_names') or [], limit=2000),
'input_tokens': usage.get('input_tokens') if usage else None,
'cached_input_tokens': usage.get('cached_input_tokens') if usage else None,
+ 'cache_creation_input_tokens': usage.get('cache_creation_input_tokens') if usage else None,
'output_tokens': usage.get('output_tokens') if usage else None,
'total_tokens': usage.get('total_tokens') if usage else None,
+ 'cost_usd': usage.get('cost_usd') if usage else None,
'duration_seconds': max(0.0, end_time - start_time),
}
)
_laminar_set_span_attributes(_terminal_laminar_gen_ai_attributes(span_input, span_output, usage))
- _laminar_set_span_output(span_output)
+ _laminar_set_span_output(span_output_messages)
if len(ranges) > max_spans:
_laminar_event(
'rust_core.llm_spans_truncated',
diff --git a/browser_use/tokens/service.py b/browser_use/tokens/service.py
index 349e51b89..dc56ddb4f 100644
--- a/browser_use/tokens/service.py
+++ b/browser_use/tokens/service.py
@@ -420,6 +420,8 @@ class TokenCost:
total_prompt_cost=0.0,
total_prompt_cached_tokens=0,
total_prompt_cached_cost=0.0,
+ total_prompt_cache_creation_tokens=0,
+ total_prompt_cache_creation_cost=0.0,
total_completion_tokens=0,
total_completion_cost=0.0,
total_tokens=0,
@@ -432,12 +434,14 @@ class TokenCost:
total_completion = sum(u.usage.completion_tokens for u in filtered_usage)
total_tokens = total_prompt + total_completion
total_prompt_cached = sum(u.usage.prompt_cached_tokens or 0 for u in filtered_usage)
+ total_prompt_cache_creation = sum(u.usage.prompt_cache_creation_tokens or 0 for u in filtered_usage)
# Calculate per-model stats with record-by-record cost calculation
model_stats: dict[str, ModelUsageStats] = {}
total_prompt_cost = 0.0
total_completion_cost = 0.0
total_prompt_cached_cost = 0.0
+ total_prompt_cache_creation_cost = 0.0
for entry in filtered_usage:
if entry.model not in model_stats:
@@ -457,6 +461,7 @@ class TokenCost:
total_prompt_cost += cost.prompt_cost
total_completion_cost += cost.completion_cost
total_prompt_cached_cost += cost.prompt_read_cached_cost or 0
+ total_prompt_cache_creation_cost += cost.prompt_cache_creation_cost or 0
# Calculate averages
for stats in model_stats.values():
@@ -468,10 +473,12 @@ class TokenCost:
total_prompt_cost=total_prompt_cost,
total_prompt_cached_tokens=total_prompt_cached,
total_prompt_cached_cost=total_prompt_cached_cost,
+ total_prompt_cache_creation_tokens=total_prompt_cache_creation,
+ total_prompt_cache_creation_cost=total_prompt_cache_creation_cost,
total_completion_tokens=total_completion,
total_completion_cost=total_completion_cost,
total_tokens=total_tokens,
- total_cost=total_prompt_cost + total_completion_cost + total_prompt_cached_cost,
+ total_cost=total_prompt_cost + total_completion_cost,
entry_count=len(filtered_usage),
by_model=model_stats,
)
diff --git a/browser_use/tokens/views.py b/browser_use/tokens/views.py
index 5a3deb14f..3e96d6327 100644
--- a/browser_use/tokens/views.py
+++ b/browser_use/tokens/views.py
@@ -99,6 +99,8 @@ class UsageSummary(BaseModel):
total_prompt_cached_tokens: int
total_prompt_cached_cost: float
+ total_prompt_cache_creation_tokens: int = 0
+ total_prompt_cache_creation_cost: float = 0.0
total_completion_tokens: int
total_completion_cost: float
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 4da30ca7b..07d25dac6 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -1713,11 +1713,13 @@ async def test_rust_terminal_usage_prices_token_count_events(monkeypatch):
assert summary.total_prompt_tokens == 300
assert summary.total_prompt_cached_tokens == 20
+ assert summary.total_prompt_cache_creation_tokens == 0
assert summary.total_completion_tokens == 30
assert summary.total_tokens == 330
assert summary.entry_count == 2
assert summary.total_prompt_cost == pytest.approx(0.000846)
assert summary.total_prompt_cached_cost == pytest.approx(0.000006)
+ assert summary.total_prompt_cache_creation_cost == pytest.approx(0.0)
assert summary.total_completion_cost == pytest.approx(0.00045)
assert summary.total_cost == pytest.approx(0.001296)
assert summary.by_model['claude-sonnet-4-6'].cost == pytest.approx(0.001296)
@@ -1750,16 +1752,138 @@ async def test_rust_terminal_usage_prices_anthropic_raw_cache_reads(monkeypatch)
assert summary.total_prompt_tokens == 183262
assert summary.total_prompt_cached_tokens == 183250
+ assert summary.total_prompt_cache_creation_tokens == 44
assert summary.total_completion_tokens == 3088
assert summary.total_tokens == 186394
assert summary.total_prompt_cost == pytest.approx(0.055176)
assert summary.total_prompt_cached_cost == pytest.approx(0.054975)
+ assert summary.total_prompt_cache_creation_cost == pytest.approx(0.000165)
assert summary.total_completion_cost == pytest.approx(0.04632)
assert summary.total_cost == pytest.approx(0.101496)
assert summary.by_model['claude-sonnet-4-6'].prompt_tokens == 183262
assert summary.by_model['claude-sonnet-4-6'].cost == pytest.approx(0.101496)
+async def test_rust_terminal_usage_sums_token_count_cache_creation(monkeypatch):
+ from browser_use.rust.service import _usage_from_events_with_costs
+ from browser_use.tokens.service import TokenCost
+
+ async def fail_fetch(_self):
+ raise AssertionError('custom model pricing should not fetch remote pricing')
+
+ monkeypatch.setattr(TokenCost, '_fetch_and_cache_pricing_data', fail_fetch)
+
+ events = [
+ {
+ 'event_type': 'token_count',
+ 'payload': {
+ 'info': {
+ 'last_token_usage': {
+ 'cached_input_tokens': 0,
+ 'input_cache_creation_tokens': 18462,
+ 'input_tokens': 3,
+ 'output_tokens': 71,
+ 'total_tokens': 18536,
+ },
+ 'total_token_usage': {
+ 'cached_input_tokens': 0,
+ 'input_cache_creation_tokens': 18462,
+ 'input_tokens': 3,
+ 'output_tokens': 71,
+ 'total_tokens': 18536,
+ },
+ },
+ },
+ },
+ {
+ 'event_type': 'token_count',
+ 'payload': {
+ 'info': {
+ 'last_token_usage': {
+ 'cached_input_tokens': 18462,
+ 'input_cache_creation_tokens': 1506,
+ 'input_tokens': 18463,
+ 'output_tokens': 177,
+ 'total_tokens': 20146,
+ },
+ 'total_token_usage': {
+ 'cached_input_tokens': 18462,
+ 'input_tokens': 18466,
+ 'output_tokens': 248,
+ 'total_tokens': 38682,
+ },
+ },
+ },
+ },
+ {
+ 'event_type': 'token_count',
+ 'payload': {
+ 'info': {
+ 'last_token_usage': {
+ 'cached_input_tokens': 18462,
+ 'input_cache_creation_tokens': 5067,
+ 'input_tokens': 18463,
+ 'output_tokens': 182,
+ 'total_tokens': 23712,
+ },
+ 'total_token_usage': {
+ 'cached_input_tokens': 36924,
+ 'input_tokens': 36929,
+ 'output_tokens': 430,
+ 'total_tokens': 62394,
+ },
+ },
+ },
+ },
+ ]
+
+ summary = await _usage_from_events_with_costs(events, 'claude-sonnet-4-6', TokenCost(include_cost=True))
+
+ assert summary.total_prompt_tokens == 36929
+ assert summary.total_prompt_cached_tokens == 36924
+ assert summary.total_prompt_cache_creation_tokens == 25035
+ assert summary.total_completion_tokens == 430
+ assert summary.total_tokens == 62394
+ assert summary.total_prompt_cost == pytest.approx(0.10497345)
+ assert summary.total_prompt_cached_cost == pytest.approx(0.0110772)
+ assert summary.total_prompt_cache_creation_cost == pytest.approx(0.09388125)
+ assert summary.total_completion_cost == pytest.approx(0.00645)
+ assert summary.total_cost == pytest.approx(0.11142345)
+
+
+async def test_rust_token_summary_does_not_double_count_cache_reads(monkeypatch):
+ from browser_use.llm.views import ChatInvokeUsage
+ from browser_use.tokens.service import TokenCost
+
+ async def fail_fetch(_self):
+ raise AssertionError('custom model pricing should not fetch remote pricing')
+
+ monkeypatch.setattr(TokenCost, '_fetch_and_cache_pricing_data', fail_fetch)
+
+ token_cost = TokenCost(include_cost=True)
+ token_cost.add_usage(
+ 'claude-sonnet-4-6',
+ ChatInvokeUsage(
+ prompt_tokens=110,
+ prompt_cached_tokens=100,
+ prompt_cache_creation_tokens=40,
+ prompt_image_tokens=None,
+ completion_tokens=20,
+ total_tokens=170,
+ ),
+ )
+
+ summary = await token_cost.get_usage_summary()
+
+ assert summary.total_prompt_cost == pytest.approx(0.00021)
+ assert summary.total_prompt_cached_cost == pytest.approx(0.00003)
+ assert summary.total_prompt_cache_creation_tokens == 40
+ assert summary.total_prompt_cache_creation_cost == pytest.approx(0.00015)
+ assert summary.total_completion_cost == pytest.approx(0.0003)
+ assert summary.total_cost == pytest.approx(0.00051)
+ assert summary.by_model['claude-sonnet-4-6'].cost == pytest.approx(summary.total_cost)
+
+
def test_rust_history_supports_structured_output():
from browser_use.rust.service import _history_from_events
@@ -7344,14 +7468,22 @@ def test_rust_agent_laminar_run_summary_populates_current_span(monkeypatch):
},
'llm_input': {
'system': [{'text': 'System prompt'}],
- 'messages': [
- {
- 'role': 'user',
- 'content': [{'type': 'text', 'text': 'Find the title on example.com'}],
- }
- ],
- 'message_count': 1,
- 'omitted_earlier_messages': 0,
+ 'messages': [
+ {
+ 'role': 'user',
+ 'content': [
+ {'type': 'text', 'text': 'Find the title on example.com'},
+ {
+ 'type': 'media',
+ 'mime_type': 'image/png',
+ 'data': 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB',
+ 'detail': 'low',
+ },
+ ],
+ }
+ ],
+ 'message_count': 1,
+ 'omitted_earlier_messages': 0,
},
},
},
@@ -7397,22 +7529,37 @@ def test_rust_agent_laminar_run_summary_populates_current_span(monkeypatch):
assert FakeLaminar.events[-1][0] == 'agent.run.terminal_summary'
assert FakeLaminar.spans[0]['name'] == 'rust_core.llm'
assert FakeLaminar.spans[0]['span_type'] == 'LLM'
- assert FakeLaminar.spans[0]['input']['tools_count'] == 2
- assert FakeLaminar.spans[0]['input']['messages'][0]['content'][0]['text'] == 'Find the title on example.com'
+ assert FakeLaminar.spans[0]['input'][0]['role'] == 'system'
+ assert FakeLaminar.spans[0]['input'][0]['content'][0]['text'] == 'System prompt'
+ assert FakeLaminar.spans[0]['input'][1]['content'][0]['text'] == 'Find the title on example.com'
+ assert FakeLaminar.spans[0]['input'][1]['content'][1] == {
+ 'type': 'image_url',
+ 'image_url': {
+ 'url': 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB',
+ 'detail': 'low',
+ },
+ }
span_attrs = {}
for attributes in FakeLaminar.spans[0]['attributes']:
span_attrs.update(attributes)
+ assert span_attrs['tools_count'] == 2
+ assert 'browser' in span_attrs['tool_names']
assert span_attrs['input_tokens'] == 100
assert span_attrs['output_tokens'] == 20
assert span_attrs['total_tokens'] == 120
assert span_attrs['gen_ai.operation.name'] == 'chat'
assert span_attrs['gen_ai.request.model'] == 'gpt-test'
- assert 'Find the title on example.com' in span_attrs['gen_ai.input.messages']
+ assert span_attrs['gen_ai.prompt.0.role'] == 'system'
+ assert span_attrs['gen_ai.prompt.0.content'] == 'System prompt'
+ assert span_attrs['gen_ai.prompt.1.role'] == 'user'
+ assert span_attrs['gen_ai.prompt.1.content'] == 'Find the title on example.com\n[image]'
+ assert span_attrs['gen_ai.completion.0.role'] == 'assistant'
+ assert span_attrs['gen_ai.completion.0.content'] == 'laminar answer'
assert span_attrs['gen_ai.usage.input_tokens'] == 100
assert span_attrs['gen_ai.usage.output_tokens'] == 20
assert span_attrs['gen_ai.usage.total_tokens'] == 120
- assert FakeLaminar.spans[0]['outputs'][-1]['assistant_output_preview'] == 'laminar answer'
- assert FakeLaminar.spans[0]['outputs'][-1]['messages'][0]['content'][0]['text'] == 'laminar answer'
+ assert span_attrs['assistant_output_preview'] == 'laminar answer'
+ assert FakeLaminar.spans[0]['outputs'][-1][0]['content'][0]['text'] == 'laminar answer'
async def test_rust_agent_authenticate_cloud_sync_logs_browser_use_warning(monkeypatch):