The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): read auxiliary hook usage as chat completions
kshitijk4poor9a773171d192
diff --git a/agent/auxiliary_hooks.py b/agent/auxiliary_hooks.pyindex 30008bf13d9..efcc54ac3e8 100644--- a/agent/auxiliary_hooks.py+++ b/agent/auxiliary_hooks.py@@ -54,7 +54,7 @@ def _system_prompt(messages: Any, kwargs: dict[str, Any]) -> str:return ""-def _usage_summary(response: Any, *, provider: str, api_mode: str) -> Optional[dict[str, Any]]:+def _usage_summary(response: Any) -> Optional[dict[str, Any]]:raw_usage = getattr(response, "usage", None)if response is None or not raw_usage:return None@@ -62,7 +62,10 @@ def _usage_summary(response: Any, *, provider: str, api_mode: str) -> Optional[dfrom agent.usage_pricing import normalize_usage- cu = normalize_usage(raw_usage, provider=provider, api_mode=api_mode)+ # Every auxiliary attempt returns from ``chat.completions.create``: native adapters+ # (Codex Responses, Anthropic Messages) translate their usage to prompt/completion+ # tokens first, so the native api_mode/provider shape would read zeros here.+ cu = normalize_usage(raw_usage, api_mode="chat_completions")summary = asdict(cu)summary.pop("raw_usage", None)summary["prompt_tokens"] = cu.prompt_tokens@@ -93,8 +96,6 @@ class _AuxCallHooks:self, *, aux_task: str, metadata: dict[str, Any], client: Any, kwargs: dict[str, Any],provider: str, model: str, api_mode: str, streaming: bool,) -> None:- self.provider = provider- self.api_mode = api_modeself.streaming = streamingself.kwargs = kwargsself.started_at = time.time()@@ -160,7 +161,7 @@ class _AuxCallHooks:payload.update(finish_reason=finish_reason,response_model=_field(response, "model"),- usage=_usage_summary(response, provider=self.provider, api_mode=self.api_mode),+ usage=_usage_summary(response),response=_Sanitize._sanitize_hook_payload({"model": _field(response, "model"),"finish_reason": finish_reason,diff --git a/tests/agent/test_auxiliary_hooks.py b/tests/agent/test_auxiliary_hooks.pyindex d5b48c12265..3129135273d 100644--- a/tests/agent/test_auxiliary_hooks.py+++ b/tests/agent/test_auxiliary_hooks.py@@ -24,7 +24,7 @@ def manager(monkeypatch):@pytest.fixture-def aux_client(monkeypatch):+def aux_client(request, monkeypatch):client = MagicMock()client.base_url = "https://openrouter.ai/api/v1"client.chat.completions.create.return_value = SimpleNamespace(@@ -33,13 +33,16 @@ def aux_client(monkeypatch):finish_reason="stop")],usage=SimpleNamespace(prompt_tokens=10, completion_tokens=5),)+ api_mode = getattr(request, "param", None)monkeypatch.setattr("agent.auxiliary_client._resolve_task_provider_model",- lambda *a, **k: ("openrouter", "mock-model", None, None, None))+ lambda *a, **k: ("openrouter", "mock-model", None, None, api_mode))monkeypatch.setattr("agent.auxiliary_client._get_cached_client", lambda *a, **k: (client, "mock-model"))monkeypatch.setattr("agent.auxiliary_client._validate_llm_response", lambda resp, _task, **_kw: resp)return client+# Native adapters hand back chat-shaped usage whatever wire they speak to the provider.[email protected]("aux_client", [None, "codex_responses", "anthropic_messages"], indirect=True)def test_call_llm_emits_auxiliary_events_not_api_request_events(manager, aux_client): # manager: PluginContextfired = []for name in ("pre_auxiliary_call", "post_auxiliary_call", "pre_api_request", "post_api_request"):