Tracing and Observability for AI Systems › Cost and Quality as Derived Views
Language AI HandbookAll chapters Cost and quality are not separate data; they are computed from, or attached to, spans. We version the price table so historical costs remain interpretable. The version stamp on a cost record is not bureaucratic overhead; without it, a price cut mid-quarter makes it impossible to tell whether your bill fell because you optimized or because your provider reduced prices, and an analysis that mixes both price regimes silently produces an average of two incompatible numbers.
PythonIn [19]Expand
# Prices in USD per 1M tokens, versioned. Fictional models.
PRICE_TABLE = {
"v2025-01": {
"gen-large": {"input": 3.00, "output": 15.00},
"embed-v3": {"input": 0.02, "output": 0.00},
"rerank-v2": {"input": 0.05, "output": 0.00},
}
}
def span_cost_usd(span: Span, price_version: str) -> float | None:
if span.kind not in ("llm", "embedding", "reranker"):
return None
model = span.attributes.get("model")
prices = PRICE_TABLE[price_version].get(model)
pt = span.attributes.get("prompt_tokens")
ct = span.attributes.get("completion_tokens")
if prices is None or pt is None or ct is None:
return None
return (pt * prices["input"] + ct * prices["output"]) / 1_000_000
def trace_cost_usd(trace: Trace, price_version: str) -> float:
costs = [span_cost_usd(span, price_version) for span in trace.spans]
return sum(cost for cost in costs if cost is not None)ConsoleOut [20]Expand
Priced spans (USD): {'generate_answer': 0.00453, 'generate_final': 0.00402}
Unpriced operations: ['embed_query', 'rag_agent.run', 'rerank', 'tool.get_balance', 'vector_search']
Estimated charges for priced spans (USD): 0.00855The example prices two generations using fictional rates. At USD 3 per million input tokens and USD 15 per million output tokens, their estimated charges are USD 0.00453 and USD 0.00402. Track whether each span is priced using its trace ID and span ID. This helper returns None for unpriced operations or missing usage. An unknown price-table version raises KeyError for a priced span kind. The estimate omits cache tiers, tool fees, and charges without usable token observations.
Quality scores attach to the span or trace that was evaluated. The target identifies what was scored. Establishing the cause of a poor score requires separate evidence. Keeping them as independent records with a source field preserves the scores from a heuristic, a judge, and a human reviewer when they disagree.
PythonIn [21]Expand
@dataclass
class QualitySignal:
trace_id: str
target_span_id: str
metric: str
score: float
source: str # heuristic | judge | user
def attach_quality_example(trace: Trace) -> list[QualitySignal]:
return [
QualitySignal(
trace.trace_id,
make_id("s", 3),
"retrieval_recall_at_3",
1.0,
"heuristic",
),
QualitySignal(
trace.trace_id, make_id("s", 7), "groundedness", 0.92, "judge"
),
QualitySignal(
trace.trace_id, make_id("s", 7), "helpfulness", 1.0, "user"
),
]
quality = attach_quality_example(example_trace)ConsoleOut [22]Expand
retrieval_recall_at_3 span=s-0003 score=1.0 source=heuristic groundedness span=s-0007 score=0.92 source=judge helpfulness span=s-0007 score=1.0 source=user
These quality numbers are invented labels. A recall of 1 for a request with a single labeled relevant document means that document was found in the three results. Low groundedness would then suggest inspecting which evidence reached generation, how it was used, and whether the evaluator scored the expected behavior.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!