Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(providers): read OpenRouter's search count from server_tool_use_details

Rufus0a58997af0af
diff --git a/packages/core/openexecutive/architecture/architecture-facts.yaml b/packages/core/openexecutive/architecture/architecture-facts.yaml
index 0323266a..5ec99746 100644
--- a/packages/core/openexecutive/architecture/architecture-facts.yaml
+++ b/packages/core/openexecutive/architecture/architecture-facts.yaml
@@ -139,9 +139,13 @@ caching:
thing on both deployment types; on either path a multi-step chat turn
gets the cap afresh on each step),
WEB_SEARCH_ALLOWED_DOMAINS / BLOCKED_DOMAINS map to allowed_domains /
- excluded_domains, and the response's ``usage.web_search_requests`` is
+ excluded_domains, and the response's search count
+ (``usage.server_tool_use_details.web_search_requests`` on the wire; the
+ flat ``usage.web_search_requests`` the docs show is accepted too) is
surfaced as ``server_tool_use`` so the usage rows record searches on
- this path too. The engine is left on OpenRouter's ``auto`` (native
+ this path too. The searches themselves never appear as ``tool_calls``;
+ their results come back as ``annotations`` (url_citation) on the
+ message. The engine is left on OpenRouter's ``auto`` (native
provider search for Claude / GPT / Gemini / Grok, Exa otherwise).
OpenRouter search's ``<cite>`` markup is stripped from tool-call
arguments before they are parsed, so a citation inside an
diff --git a/packages/core/openexecutive/providers/translator.py b/packages/core/openexecutive/providers/translator.py
index d18d12fc..52accac8 100644
--- a/packages/core/openexecutive/providers/translator.py
+++ b/packages/core/openexecutive/providers/translator.py
@@ -685,10 +685,19 @@ def from_openai_response(body: dict[str, Any]) -> SimpleNamespace:
def _server_tool_usage(usage: dict[str, Any]) -> SimpleNamespace:
- """Anthropic-shape ``usage.server_tool_use`` from OpenRouter's flat
- ``usage.web_search_requests`` (present when the request carried the
- ``openrouter:web_search`` server tool; 0 otherwise)."""
- raw = usage.get("web_search_requests", 0)
+ """Anthropic-shape ``usage.server_tool_use`` from OpenRouter's usage.
+
+ On the wire the count sits under ``usage.server_tool_use_details``
+ (``{"web_search_requests": N, "tool_calls_requested": N,
+ "tool_calls_executed": N}``, observed on the chat-completions endpoint);
+ a flat ``usage.web_search_requests`` (the shape the docs show) is
+ accepted too. 0 when neither is present."""
+ details = usage.get("server_tool_use_details")
+ raw = (
+ details.get("web_search_requests", 0)
+ if isinstance(details, dict)
+ else usage.get("web_search_requests", 0)
+ )
try:
count = int(raw or 0)
except (TypeError, ValueError, OverflowError):
diff --git a/packages/core/tests/unit/test_openrouter_translator.py b/packages/core/tests/unit/test_openrouter_translator.py
index 8d776a91..63a6fb11 100644
--- a/packages/core/tests/unit/test_openrouter_translator.py
+++ b/packages/core/tests/unit/test_openrouter_translator.py
@@ -337,9 +337,17 @@ def test_response_reports_search_count_and_skips_server_tool_calls() -> None:
"function": {"name": "emit_research_findings", "arguments": "{\"findings\": []}"}},
],
}}],
- "usage": {"prompt_tokens": 10, "completion_tokens": 2, "web_search_requests": 3},
+ "usage": {"prompt_tokens": 10, "completion_tokens": 2,
+ "server_tool_use_details": {"web_search_requests": 3, "tool_calls_executed": 3}},
})
assert msg.usage.server_tool_use.web_search_requests == 3
+ # The flat shape the docs show is accepted as well.
+ flat = from_openai_response({
+ "id": "f", "model": "m",
+ "choices": [{"finish_reason": "stop", "message": {"role": "assistant", "content": "ok"}}],
+ "usage": {"prompt_tokens": 1, "completion_tokens": 1, "web_search_requests": 2},
+ })
+ assert flat.usage.server_tool_use.web_search_requests == 2
assert [b.name for b in msg.content if b.type == "tool_use"] == ["emit_research_findings"]
assert msg.stop_reason == "tool_use" # a client tool call remains
@@ -1057,7 +1065,8 @@ def test_stream_accumulator_reports_search_count() -> None:
acc = StreamAccumulator()
acc.feed({"id": "s", "model": "m", "choices": [{"delta": {"content": "hi"}, "finish_reason": None}]})
acc.feed({"id": "s", "model": "m", "choices": [{"delta": {}, "finish_reason": "stop"}],
- "usage": {"prompt_tokens": 5, "completion_tokens": 1, "web_search_requests": 2}})
+ "usage": {"prompt_tokens": 5, "completion_tokens": 1,
+ "server_tool_use_details": {"web_search_requests": 2}}})
msg = acc.finalize()
assert msg.usage.server_tool_use.web_search_requests == 2