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 ¶
Bases: MetricsExecutionMixin, MetricsQueryMixin
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 symfonic/core/observability/metrics.py
on_agent_end
async
¶
Increment turn count and purge run-scoped bookkeeping.
Source code in 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 symfonic/core/observability/metrics.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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 | |
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 symfonic/core/observability/metrics.py
set_lineage ¶
Associate one execution with its delegation root and parent.
Source code in 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 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, root_run_id: str = '', parent_run_id: str | None = None)
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.