All articles
AI Insights

Context Engineering for AI Agents: A How-To

Prompt engineering asked “what should I say to the model?” Context engineering asks a better question: “what should be in the model’s context window at this moment, and what should not?” For AI agents — systems that run over many turns, call tools, and accumulate history — that question decides whether the agent stays sharp or slowly degrades as its window fills with noise. This how-to walks through the practical steps we apply when building agents at Neomeric, a Melbourne-based AI product and consulting company — and the team behind NeoMind, Australia’s onshore AI teammates platform.

What is context engineering?

Context engineering is the discipline of curating the set of tokens a model sees at inference time — system prompt, tool definitions, retrieved documents, message history, and tool results — so that every token earns its place. Anthropic’s engineering guide frames it as the natural progression of prompt engineering: context is a critical but finite resource, and the job is to find the configuration of context most likely to produce the behaviour you want. The failure mode it prevents is familiar to anyone who has run a long agent session: the model that was precise at turn three becomes vague at turn thirty, not because the model changed but because its window filled with stale tool output and irrelevant history.

Step 1 — Audit what is actually in the window

Before optimising anything, log the full assembled context of a few real requests and read it. Most teams are surprised: duplicated instructions, entire documents retrieved for a one-line answer, verbose tool results kept verbatim across turns. You cannot curate what you have not seen. Make context inspection a debugging habit, the way you would read a SQL query plan before tuning a database.

Want the numbers before you build?

Get the free Australian AI MVP Cost Guide 2026 — we’ll email it straight to you.

Step 2 — Write the system prompt at the right altitude

The system prompt should sit between two failure modes: hardcoded if-else brittleness at one extreme, and vague high-level guidance at the other. State the agent’s role, its non-negotiable rules, and the shape of a good outcome — then stop. Every edge case you enumerate in prose is an edge case the model may over-fit to; every rule you leave implicit is one it may miss. Start minimal, add instructions only in response to observed failures, and treat the prompt as versioned code with an eval suite behind it — our guide to testing AI products with evals covers how to catch regressions when you edit it.

Free: The Australian AI MVP Cost Guide 2026

Honest cost benchmarks, the hidden costs vendors don’t quote, and a 10-line scoping worksheet.

Get the free guide

Step 3 — Give the agent fewer, sharper tools

Tools are context too: every definition consumes tokens and every overlapping capability creates a decision point where the model can choose wrong. Prefer a small set of tools with crisp, non-overlapping purposes and descriptions written like good documentation — what it does, when to use it, what it returns. If a human engineer would need to ask which of two tools applies, the model will guess. Constrain tool results as well: return the fields the task needs, not the full API payload. We cover the architectural side in AI agent design patterns.

Step 4 — Retrieve just in time, not just in case

Loading every possibly relevant document up front is the most common context-bloat mistake. The better pattern is just-in-time retrieval: keep lightweight identifiers — file paths, record IDs, search queries — and let the agent fetch content when the task actually requires it. Retrieval quality then becomes the bottleneck, which is a pipeline problem: chunking, freshness, and hybrid search, covered in our RAG data pipeline how-to and the underlying RAG architecture guide.

Step 5 — Compact long histories deliberately

For tasks that outlive the window, decide what survives: summarise completed sub-tasks, keep decisions and constraints, drop raw tool output that has already been acted on. A useful pattern is structured note-taking — the agent maintains a short running summary of state outside the message history, and stale turns are pruned. Compaction is lossy by design; the engineering judgement is choosing what the future turns will actually need.

Step 6 — Cache the stable prefix

Order the context so that stable content — system prompt, tool definitions, reference material — comes first and volatile content last, then use provider prompt caching on the stable prefix. Anthropic’s prompt caching reduces costs by up to 90% and latency by up to 85% for long prompts, and equivalents exist across major providers. Caching rewards exactly the discipline this guide teaches: a well-factored context with a stable prefix is also the cheapest one to serve. For the wider cost picture, see our guide to AI API cost optimisation.

Step 7 — Validate the output side too

Context engineering governs what goes in; it pairs with guardrails governing what comes out. Schema-constrained outputs, semantic validation, and human escalation triggers turn a well-fed agent into a trustworthy one — we cover that half of the discipline in this week’s companion piece on AI guardrails and structured outputs.

Frequently asked questions

How is context engineering different from prompt engineering?

Prompt engineering focuses on writing effective instructions. Context engineering manages everything the model sees at inference time — system prompt, tools, retrieved documents, history, and tool results — and curates it across every turn of an agent’s operation, treating context as a finite resource.

Why do agents get worse over long sessions?

Usually because the context window fills with stale tool output, duplicated instructions, and irrelevant history. The model is not degrading; its inputs are. Compaction, just-in-time retrieval, and pruning keep long-running agents sharp.

Does a bigger context window solve this?

No. Larger windows raise the ceiling but do not remove the need for curation — irrelevant tokens still dilute attention, add cost, and slow responses. Efficient context use matters at every window size.

What is the quickest win for an existing agent?

Log and read the fully assembled context of real requests, then remove what is not earning its place — verbose tool results and just-in-case documents are the usual offenders. Ordering stable content first and enabling prompt caching typically follows as the second win.

Sources

Building something? Get a straight answer on cost.

Neomeric is a Melbourne AI product studio — 7+ products shipped, including our own. Start with a free 15-minute scoping call, or a 2-week Build Sprint at A$6,900 fixed, fully credited toward your pilot.

Book a free scoping callDownload the cost guide

Disclaimer: This article is general information only, current at the time of writing, and is not legal, financial or professional advice. Regulatory obligations, pricing and market figures change and vary by circumstance — seek advice specific to your situation before acting. Statistics cited are drawn from the third-party sources linked in this article; Neomeric is not responsible for third-party content.

AI Insights AI Development AI Strategy
PDF · Free

Get the Australian AI MVP Cost Guide 2026.

What an AI MVP really costs in Australia in 2026 — line-item budgets, the traps that blow them out, and how to scope a build that pays for itself.

N
Neomeric Team

We build the AI products others can’t. Melbourne, Australia. Work with us →