Why Agents Fail in Production
Agents fail in the seams โ not in the core LLM call, but in the transitions between steps. A tool returns unexpected JSON. An API rate-limits. The agent loses track of its goal after 3 tool calls. These are the failure modes that don't show up in demos.
Adopt trace-first debugging: every run produces a timeline with inputs, outputs, costs, and tool calls. This reduces mean-time-to-resolution (MTTR) dramatically when something goes wrong at 2am.
Architecture Principles
1. Task Decomposition
Break complex tasks into small, verifiable sub-tasks. Each sub-task should have a clear success criterion the agent can check. "Summarize this document" is a good sub-task; "Be helpful" is not.
2. Deterministic Tools
Tools should be pure functions where possible. Same input โ same output. Avoid side effects in read operations. Constrain outputs with JSON schemas โ don't let the agent make up field names.
3. Sandbox Side-Effects
Write operations (sending emails, creating database records, making payments) should require explicit confirmation. Implement compensating actions for partial failures โ if step 3 fails, you need to know how to undo steps 1 and 2.
4. Guardrails
Add input and output guardrails. Input guardrails check for prompt injection and off-topic requests. Output guardrails verify the response doesn't contain PII, harmful content, or hallucinated claims.
Observability Stack
Instrument every tool call with:
- Input/output logging โ what was the prompt, what came back
- Latency โ how long each step took
- Token count โ cost per step
- Tool invocation count โ detect infinite loops early
- Error type โ distinguish model errors from tool errors from network errors
Rollback Plans
For every write operation in your agent, answer these questions before deployment:
- What happens if this fails halfway through?
- Can we detect partial completion?
- What's the compensating action?
- Who gets notified if auto-rollback fails?
Production Checklist
- โ Tracing enabled for all tool calls
- โ JSON schema validation on all tool inputs/outputs
- โ Maximum tool call limit configured per run
- โ Compensating actions documented for write operations
- โ Human escalation path for high-stakes decisions
- โ Separate staging environment with real data samples
- โ Rollback procedure tested before going live