agents/prod
13
Operations · Chapter 13

Evaluation, observability, security & cost

Getting an agent to work in a demo is easy. Keeping it correct, debuggable, safe, and affordable in production is the actual job — and it's four distinct disciplines. Skip them and you ship something that dazzles in a screenshot and quietly fails in the field (see: Klarna's long-tail walk-back).

1 · Evaluation — you can't ship what you can't measure

The critical insight: output-only evaluation is insufficient — a correct final answer can hide broken reasoning or a hallucinated tool call. So agents are evaluated at three granularities:

end-to-end (black-box): did the task succeed? trajectory (glass-box): was the tool path right? single-step (white-box): was each decision correct? zoom in to isolate exactly where it broke
Evaluate the whole trajectory, not just the answer. LLM-as-a-judge reaches >80% agreement with humans but has position/verbosity/self-enhancement biases — calibrate it against human labels first.

Hallucination isn't one metric — it's four, and they can disagree on the same answer:

Groundedness — supported by the retrieved chunks?
Faithfulness — true to the source text?
Factuality — correct against the world?
Consistency — do repeats agree?

2 · Observability — tracing the tree

A single agent request is a tree of prompts, model calls, tools, retries, and sub-agents — plain logs can't expose it. The standard is now OpenTelemetry GenAI conventions: a top-level invoke_agent span with child chat and execute_tool spans. Store prompts as span events (not attributes — they're indexed and leak PII), and propagate trace context across every hop.

invoke_agent · 1.9s chat (LLM) 0.5s execute_tool 0.9s ← slow chat 0.4s token/cost/latency per span → find the slow hop instantly
The waterfall shows where time and tokens go. Tools: LangSmith, Langfuse, Arize Phoenix, Braintrust.

3 · Security — the lethal trifecta

Prompt injection is OWASP's #1 LLM risk — the model processes instructions and data in the same channel, so anyone who controls data the model reads can inject commands. The most useful mental model (from Ch9): data exfiltration needs three things at once, and breaking any one stops it.

private datauntrustedegress breach
Restrict tools to least privilege, require human approval for risky actions, and enforce guardrails at the AI gateway. Never treat the system prompt as a secret.

4 · Cost optimization — stack the levers

Unoptimized agent workflows waste 40–60% of tokens. The big levers stack — toggle them on a nominal $1.00 bill:

Interactive · stack the cost levers
The operations mindset (and the finale)

Evaluate the whole trajectory, trace every span, assume every input is hostile and break the lethal trifecta, and route/cache aggressively. That's the difference between a demo and a product. This closes the core stack — the MLOps chapter zooms out to the platform and infra that runs it all.