mirror of
https://github.com/usestrix/strix.git
synced 2026-08-22 11:02:08 +02:00
Record usage per SDK LLM response
This commit is contained in:
+15
-33
@@ -21,6 +21,7 @@ if TYPE_CHECKING:
|
||||
from pathlib import Path
|
||||
|
||||
from agents.items import TResponseInputItem
|
||||
from agents.lifecycle import RunHooks
|
||||
from agents.memory import Session, SQLiteSession
|
||||
from agents.result import RunResultBase
|
||||
|
||||
@@ -30,7 +31,6 @@ if TYPE_CHECKING:
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
StreamEventSink = Callable[[str, Any], None]
|
||||
UsageSink = Callable[[str, str | None, Any], None]
|
||||
|
||||
|
||||
async def run_agent_loop(
|
||||
@@ -46,7 +46,7 @@ async def run_agent_loop(
|
||||
session: Session | None = None,
|
||||
start_parked: bool = False,
|
||||
event_sink: StreamEventSink | None = None,
|
||||
usage_sink: UsageSink | None = None,
|
||||
hooks: RunHooks[dict[str, Any]] | None = None,
|
||||
) -> RunResultBase | None:
|
||||
await coordinator.attach_runtime(
|
||||
agent_id,
|
||||
@@ -68,7 +68,7 @@ async def run_agent_loop(
|
||||
session=session,
|
||||
interactive=interactive,
|
||||
event_sink=event_sink,
|
||||
usage_sink=usage_sink,
|
||||
hooks=hooks,
|
||||
)
|
||||
else:
|
||||
result = await _run_noninteractive_until_lifecycle(
|
||||
@@ -81,7 +81,7 @@ async def run_agent_loop(
|
||||
max_turns=max_turns,
|
||||
session=session,
|
||||
event_sink=event_sink,
|
||||
usage_sink=usage_sink,
|
||||
hooks=hooks,
|
||||
)
|
||||
|
||||
if not interactive:
|
||||
@@ -105,7 +105,7 @@ async def run_agent_loop(
|
||||
session=session,
|
||||
interactive=interactive,
|
||||
event_sink=event_sink,
|
||||
usage_sink=usage_sink,
|
||||
hooks=hooks,
|
||||
)
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ async def spawn_child_agent(
|
||||
skills: list[str],
|
||||
parent_history: list[Any],
|
||||
event_sink: StreamEventSink | None = None,
|
||||
usage_sink: UsageSink | None = None,
|
||||
hooks: RunHooks[dict[str, Any]] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
parent_id = parent_ctx.get("agent_id")
|
||||
if not isinstance(parent_id, str):
|
||||
@@ -161,7 +161,7 @@ async def spawn_child_agent(
|
||||
parent_history=parent_history,
|
||||
),
|
||||
event_sink=event_sink,
|
||||
usage_sink=usage_sink,
|
||||
hooks=hooks,
|
||||
)
|
||||
|
||||
return {
|
||||
@@ -185,7 +185,7 @@ async def respawn_subagents(
|
||||
parent_ctx: dict[str, Any],
|
||||
root_id: str,
|
||||
event_sink: StreamEventSink | None = None,
|
||||
usage_sink: UsageSink | None = None,
|
||||
hooks: RunHooks[dict[str, Any]] | None = None,
|
||||
) -> None:
|
||||
"""Re-spawn subagent runners from a restored coordinator snapshot."""
|
||||
async with coordinator._lock:
|
||||
@@ -242,7 +242,7 @@ async def respawn_subagents(
|
||||
initial_input=[],
|
||||
start_parked=start_parked,
|
||||
event_sink=event_sink,
|
||||
usage_sink=usage_sink,
|
||||
hooks=hooks,
|
||||
)
|
||||
logger.info(
|
||||
"respawned %s (%s) parent=%s task_len=%d",
|
||||
@@ -268,7 +268,7 @@ async def _run_noninteractive_until_lifecycle(
|
||||
max_turns: int,
|
||||
session: Session | None,
|
||||
event_sink: StreamEventSink | None,
|
||||
usage_sink: UsageSink | None,
|
||||
hooks: RunHooks[dict[str, Any]] | None,
|
||||
) -> RunResultBase | None:
|
||||
"""Non-chat mode keeps running until finish_scan / agent_finish settles status."""
|
||||
result: RunResultBase | None = None
|
||||
@@ -288,7 +288,7 @@ async def _run_noninteractive_until_lifecycle(
|
||||
session=session,
|
||||
interactive=False,
|
||||
event_sink=event_sink,
|
||||
usage_sink=usage_sink,
|
||||
hooks=hooks,
|
||||
)
|
||||
|
||||
status = await _agent_status(coordinator, agent_id)
|
||||
@@ -333,7 +333,7 @@ async def _run_cycle(
|
||||
session: Session | None,
|
||||
interactive: bool,
|
||||
event_sink: StreamEventSink | None,
|
||||
usage_sink: UsageSink | None,
|
||||
hooks: RunHooks[dict[str, Any]] | None,
|
||||
) -> RunResultBase | None:
|
||||
try:
|
||||
await coordinator.mark_running(agent_id)
|
||||
@@ -344,6 +344,7 @@ async def _run_cycle(
|
||||
context=context,
|
||||
max_turns=max_turns,
|
||||
session=session,
|
||||
hooks=hooks,
|
||||
)
|
||||
await coordinator.attach_stream(agent_id, stream)
|
||||
try:
|
||||
@@ -356,8 +357,6 @@ async def _run_cycle(
|
||||
if stream.run_loop_exception is not None:
|
||||
raise stream.run_loop_exception
|
||||
finally:
|
||||
if usage_sink is not None:
|
||||
_emit_stream_usage(agent, agent_id, stream, usage_sink)
|
||||
await coordinator.detach_stream(agent_id, stream)
|
||||
except Exception as exc:
|
||||
if not interactive:
|
||||
@@ -477,7 +476,7 @@ async def _start_child_runner(
|
||||
initial_input: Any,
|
||||
start_parked: bool = False,
|
||||
event_sink: StreamEventSink | None = None,
|
||||
usage_sink: UsageSink | None = None,
|
||||
hooks: RunHooks[dict[str, Any]] | None = None,
|
||||
) -> None:
|
||||
session = open_agent_session(child_id, agents_db_path)
|
||||
sessions_to_close.append(session)
|
||||
@@ -501,25 +500,8 @@ async def _start_child_runner(
|
||||
session=session,
|
||||
start_parked=start_parked,
|
||||
event_sink=event_sink,
|
||||
usage_sink=usage_sink,
|
||||
hooks=hooks,
|
||||
),
|
||||
name=f"agent-{name}-{child_id}",
|
||||
)
|
||||
await coordinator.attach_runtime(child_id, task=task_handle)
|
||||
|
||||
|
||||
def _emit_stream_usage(
|
||||
agent: Any,
|
||||
agent_id: str,
|
||||
stream: RunResultBase,
|
||||
usage_sink: UsageSink,
|
||||
) -> None:
|
||||
context_wrapper = getattr(stream, "context_wrapper", None)
|
||||
usage = getattr(context_wrapper, "usage", None)
|
||||
if usage is None:
|
||||
return
|
||||
agent_name = getattr(agent, "name", None)
|
||||
try:
|
||||
usage_sink(agent_id, agent_name if isinstance(agent_name, str) else None, usage)
|
||||
except Exception:
|
||||
logger.exception("usage sink failed for %s", agent_id)
|
||||
|
||||
Reference in New Issue
Block a user