symfonic.core.observability.metrics¶
metrics ¶
ConversationMetricsCollector — aggregates token/cost per conversation.
Implements the CallbackHandler protocol so it can be registered directly with SymfonicAgent(metrics_collector=...) or added to any CallbackManager.
Sprint 4 memory safety: bounded call_records deque, LRU-capped
_conversations, run-scoped map purge on on_agent_end, and
observable budget-tracker fan-out errors.
ConversationMetricsCollector ¶
Aggregates token usage and cost per conversation (session_id).
Satisfies the CallbackHandler protocol via structural typing.
Usage::
collector = ConversationMetricsCollector()
agent = SymfonicAgent(..., metrics_collector=collector)
# after some runs:
record = collector.get_conversation(session_id)
all_records = collector.list_conversations()
Source code in src/symfonic/core/observability/metrics.py
get_conversation ¶
Return the record for a single conversation, or None.
list_conversations ¶
Return conversations sorted by last_seen (most recent first).
When tenant_id is provided, only conversations belonging to that
tenant are returned. The tenant filter is applied before the
limit slice so a tenant always sees up to limit of its own
conversations regardless of other tenants' activity.
Source code in src/symfonic/core/observability/metrics.py
on_agent_end
async
¶
Increment turn count and purge run-scoped bookkeeping.
Source code in src/symfonic/core/observability/metrics.py
on_llm_end
async
¶
Accumulate token/cost metrics into the matching conversation record.
Also fans the same usage out to the process-wide
:class:TokenBudgetTracker (if one has been registered via
set_budget_tracker) so per-tenant ceilings can fire on the
NEXT call. Failures in the tracker must never break the LLM flow.
Source code in src/symfonic/core/observability/metrics.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
on_token_composition
async
¶
set_conversation ¶
Associate a run_id with a conversation (session) id.
Call this before the agent run so metrics land under the right conversation bucket (e.g. use the session_id as conversation_id).
Source code in src/symfonic/core/observability/metrics.py
set_tenant ¶
Associate a run_id with a tenant_id.
Call this before the agent run so on_llm_end can fan usage
into the registered TokenBudgetTracker. Without this the
tracker never fires because the ConversationRecord's tenant_id
stays empty.
Source code in src/symfonic/core/observability/metrics.py
snapshots ¶
Return per-LLM-call metric snapshots as plain dicts.
Public, stable API for admin dashboards and downstream aggregation.
Each snapshot represents a single LLM call, enriched with the
parent conversation's tenant_id so callers can filter by tenant
without joining.
The return value is a newly constructed list of dicts — mutating it does not affect the collector's internal state.
The dict shape is intentionally permissive so dashboards can continue to work across minor releases; currently includes:
conversation_id, call_id, tenant_id, model, node,
input_tokens, output_tokens, cached_tokens, cost_usd,
call_type, timestamp, created_at, composition,
cache_creation_tokens, reasoning_tokens, duration_ms,
iteration_index, turn_index.
The last five (v8.7.11) surface the per-call detail the framework
already emits on LLMEndEvent — real node names, latency, React
iteration + user-turn index, and the cache-creation/reasoning
token splits — so a conversation-detail drill-down can render them
without an external OTel backend.
Source code in src/symfonic/core/observability/metrics.py
summary ¶
summary(
start: datetime | None = None,
end: datetime | None = None,
tenant_id: str | None = None,
) -> dict[str, Any]
Return aggregate totals across all conversations in the time window.
When tenant_id is provided only that tenant's conversations are
aggregated, preventing cross-tenant metric leakage.
Source code in src/symfonic/core/observability/metrics.py
ConversationRecord
dataclass
¶
ConversationRecord(
conversation_id: str,
tenant_id: str,
turns: int = 0,
llm_calls: int = 0,
input_tokens: int = 0,
output_tokens: int = 0,
cached_tokens: int = 0,
cost_usd: float = 0.0,
first_seen: datetime = (lambda: datetime.now(UTC))(),
last_seen: datetime = (lambda: datetime.now(UTC))(),
call_records: deque[LLMCallRecord] = (
lambda: deque(
maxlen=MAX_CALL_RECORDS_PER_CONVERSATION
)
)(),
)
Aggregated per-conversation metrics.
LLMCallRecord
dataclass
¶
LLMCallRecord(
call_id: str,
conversation_id: str,
model: str,
node: str,
input_tokens: int,
cached_tokens: int,
output_tokens: int,
cost_usd: float,
call_type: str,
timestamp: datetime,
composition: dict[str, int] = dict(),
cache_creation_tokens: int = 0,
reasoning_tokens: int = 0,
duration_ms: float = 0.0,
iteration_index: int = 0,
turn_index: int = 0,
)
Single LLM call record for dashboard display.
v8.7.11 — the record now carries the full per-call telemetry the
framework already emits on :class:LLMEndEvent (real node name,
call_type, iteration_index, turn_index, duration_ms,
and the cache_creation/reasoning token splits) instead of
hardcoding node="react" / call_type="initial" and dropping
latency. All new fields default to zero/empty so pre-v8.7.11 callers
that construct the record positionally with the original arg set keep
working unchanged.