Context Discipline Rules for Predictable Agent Behavior
Context discipline is the practice of structuring, trimming, and refreshing everything an agent sees so each decision is based on goal-relevant information instead of an unbounded transcript.
Search across all documentation pages
Context discipline is the practice of structuring, trimming, and refreshing everything an agent sees so each decision is based on goal-relevant information instead of an unbounded transcript.
Most "the model got dumber mid-run" reports are context problems: noise, contradiction, or sheer volume drowning the current objective.
| Rule | Default practice |
|---|---|
| Section stable policy | Role, goal, constraints, tool guidance, output format as labeled blocks |
| Keep policy append-reviewed | Every system-policy addition needs an owner and reason |
| One source of truth for prompts | No copy-pasted forks that drift per call site |
| Put dynamic facts in turn state | Dates, user ids, live records belong in the current user/turn payload |
| State hard constraints as testable lines | "Never call refund without approval_id" beats vague caution |
| Signal | Action |
|---|---|
| Tool result older than N turns and superseded | Drop + short marker |
| Single tool payload over size cap | Truncate/summarize at adapter |
| Approach approaching context limit | Summarize phase + hard trim |
| Failed then succeeded retry | Keep success only |
| Policy line with no owner | Candidate for deletion after test |
| Same file pasted 3+ times | Replace with reference + slice |
MAX_TOOL_RESULTS = 6
def trim_messages(messages: list[dict]) -> list[dict]:
"""Keep recent tool observations; mark older ones as omitted."""
tool_idxs = [i for i, m in enumerate(messages) if m.get("role") == "tool"]
if len(tool_idxs) <= MAX_TOOL_RESULTS:
return messages
keep_from = tool_idxs[-MAX_TOOL_RESULTS]
out = []
for i, m in enumerate(messages):
if i < keep_from and m.get("role") == "tool":
out.append({"role": "tool", "content": "[earlier tool result omitted]"})
else:
out.append(m)
return outThis sketch is illustrative only.
Production systems often use token budgets, summarization turns, or framework memory primitives - the rule is explicit policy, not a specific algorithm.
Large windows still suffer attention and cost problems.
Irrelevant history competes with the current goal and multiplies token spend every turn.
Summarization preserves gist; hard drops preserve exact recent facts.
Many systems use both: summarize old phases, keep raw recent tool results.
Prefer the tool layer so every consumer of that tool inherits the cap and logs stay honest about what the model saw.
Measure failing and successful runs.
Raise caps only when you can show missing older results caused wrong actions.
It improves reviewability and reduces self-contradiction.
Models still need clean observations; structure is not a substitute for trimming.
Short, honest, and non-instructive junk: note that content was omitted for length, not a fake tool success.
Stable system policy should stay byte-stable for cache hits.
Volatile state belongs outside the cached prefix so trims do not thrash the cache.
Yes.
If the agent re-fetches constantly or contradicts earlier commitments, raise retention or improve summaries rather than disabling discipline entirely.
Often yes for local repro.
Never leave debug retention on in production paths.
Handoffs need a curated packet (goal, constraints, key facts), not a raw dump of the prior agent's full transcript.
Usually no.
Unreviewed accretion creates contradictions.
Fix tools, goals, or evals first.
Tokens per turn broken down by policy, transcript, and tool results - so you can see what is crowding the window.
Stack versions: Pins from the category manifest (verify at build): OpenRouter (~315+ models, July 2026 pricing/fees); LangGraph 1.0+; CrewAI 1.14+; Microsoft Agent Framework 1.0; Vercel AI SDK 6; Pydantic AI (latest); LlamaIndex (latest); OpenAI Agents SDK (latest + MCP); MCP (Linux Foundation governance); A2A (HTTP+SSE+JSON-RPC 2.0); Solana
@solana/web3.js+@solana/spl-token.
Reviewed by Chris St. John·Last updated Jul 16, 2026