ChangeGamer

← All resources

Prompt and Context Engineering for Agents

Guide · updated 2026-06-16 · Markdown variant

From crafting a single prompt to managing everything an agent sees across a trajectory: system-prompt design, context-window management, failure modes, and a high-leverage checklist.


From prompt engineering to context engineering

Prompt engineering is crafting a single instruction. Context engineering is managing the entire token window an agent sees across a multi-step trajectory: system prompt, tool definitions, retrieved data, memory outputs, and accumulated message history. Anthropic defines it as "the set of strategies for curating and maintaining the optimal set of tokens during LLM inference." Cognition (Devin) calls it "the #1 job of engineers building AI agents."

The framing shift matters because agents fail not from a bad one-liner prompt but from poor information architecture across the whole window.

System-prompt design for agents

A well-structured system prompt typically covers five layers:

  1. Role + objective — who the agent is and what success looks like.
  2. Tool-use instructions — when to call which tool, and when not to.
  3. Constraints and guardrails — what the agent must never do.
  4. Output format — schema, tone, length, and structure of the response.
  5. "Right altitude" principle — specific enough to steer behavior; general enough to generalize to cases not enumerated. Avoid exhaustive rule lists; prefer canonical examples.

Anthropic's guidance: if a human engineer cannot definitively say which tool to use in a given situation, an agent cannot either — keep the toolset minimal and curated.

Core prompt-engineering techniques

Context-window management (the agent-specific layer)

For single-turn LLM calls, context is static. For agents the window is dynamic and grows across steps. Key management strategies:

Failure modes to know

Failure mode Description Mitigation
Context rot Recall degrades as window length grows Compaction; just-in-time retrieval
Lost-in-the-middle Model under-weights information in the middle of a long context Put high-priority context at the start or end; summarize the middle
Context poisoning Adversarial data injected into retrieved content or tool outputs Treat all external content as untrusted data, not instructions. See /resources/agentic-security-checklist
Context clash / distraction Conflicting instructions from different context segments Use structured delimiters; isolate sub-agent windows

Evaluation and iteration

Treat prompts and context configurations as versioned artifacts (store in version control, review in PRs). Measure changes with evals before deploying — see /resources/evaluating-ai-agents. Avoid over-fitting to one model's quirks: test on at least two models before calling a prompt stable.

High-leverage context-engineering checklist

Verified sources

#prompt-engineering #context-engineering #agents #system-prompt #context-window #few-shot #chain-of-thought

Category: Guide