Agents & Tool Use
Context Management and Reliability Patterns
Context preservation across long interactions, escalation decision-making, error propagation in multi-agent systems, and information provenance.
advanced · 10 min read · Premium
Production agents operate in long sessions, across multiple agents, and with information from many sources. Managing context, errors, and provenance is what separates reliable systems from fragile demos.
Context Preservation
Three risks threaten context quality in long interactions:
Progressive summarization condenses specific values (amounts, dates, order numbers) into vague summaries. Counter this by extracting transactional facts into a persistent "case facts" block included in each prompt outside summarized history.
Lost-in-the-middle means models process information at the beginning and end of long inputs reliably but may miss findings from middle sections. Place key findings summaries at the beginning and use explicit section headers.
Tool result accumulation fills context with irrelevant data (40+ fields per order lookup when 5 are relevant). Trim verbose outputs to only relevant fields using PostToolUse hooks before they accumulate.
Escalation Decision-Making
Escalate when: a customer explicitly requests a human agent (honor immediately), policy is ambiguous or silent, or the agent cannot make meaningful progress. Do NOT use sentiment analysis or self-reported confidence scores as escalation triggers - they're poorly calibrated and don't correlate with actual case complexity.
When tool results return multiple customer matches, ask for additional identifiers rather than selecting based on heuristics.
Error Propagation
Return structured error context (failure type, attempted query, partial results, alternative approaches) to enable intelligent coordinator recovery. Subagents should implement local recovery for transient failures and only propagate errors they cannot resolve. Never suppress errors by returning empty results as success. Never terminate entire workflows on single failures.
Information Provenance
Source attribution is lost during summarization when findings are compressed without preserving claim-source mappings. Require subagents to output structured mappings (source URLs, document names, relevant excerpts) that downstream agents preserve through synthesis. Handle conflicting statistics by annotating conflicts with source attribution rather than selecting one value. Include publication dates to prevent temporal differences from being misinterpreted as contradictions.
Keep reading with Pro.
You're reading the preview. Unlock the full concept plus the library, study plans, the AI mentor, and daily emails.