9 min read

Context Engineering: Pick the Right Tokens, Not Just Fewer

Context engineering means curating the smallest set of high-signal tokens. How data format, tool-result clearing and just-in-time retrieval work together.

By json2toon.co

Context engineering is the practice of deciding which tokens an LLM sees at each step: which tools are loaded, which results stay in history, what gets retrieved and how data is represented. The goal is not the fewest tokens. It is the smallest set of high-signal tokens that gets the task done.

What does context engineering actually mean?

The term went mainstream with Anthropic's September 29, 2025 engineering post, Effective context engineering for AI agents. It defines context engineering as "the set of strategies for curating and maintaining the optimal set of tokens (information) during LLM inference, including all the other information that may land there outside of the prompts."

The guiding principle in the same post: "good context engineering means finding the smallest possible set of high-signal tokens that maximize the likelihood of some desired outcome." The reason is what Anthropic calls an attention budget. "Every new token introduced depletes this budget by some amount," so context should be treated "as a finite resource with diminishing marginal returns."

Note the two halves of that principle. "Smallest" is about volume. "High-signal" is about selection. A data format can only help with the first. Most of the gains come from the second.

Why isn't "fewer tokens" the whole answer?

Because irrelevant tokens hurt even when they are cheap. Chroma's context rot study (July 2025, 18 models) compared LongMemEval prompts averaging about 113k tokens with focused versions of about 300 tokens that contained only the relevant parts. "Across all models, we see significantly higher performance on focused prompts compared to full prompts."

Compressing that 113k-token prompt by 40% would still leave the model searching through roughly 68k tokens of mostly irrelevant history. Cutting it down to the 300 tokens that matter is a different class of improvement. That is why a serialization format belongs at the end of a context pipeline, after you have decided what to include.

The format still matters once the selection is right. On the official TOON benchmark, the current run (spec v4.1, 5,856 calls) found TOON used 42.6% fewer tokens than JSON with 72.2% vs 71.4% accuracy. Those savings apply to every byte of data you have already decided to keep. You can check them on your own data with the TOON token calculator.

The four levers: select, retrieve, clear, encode

Every context-engineering technique for data falls into one of four levers. They compound, so order matters.

LeverQuestion it answersTypical techniqueWhat it removes
SelectWhich records and fields does this step need?Field projection, filters, GraphQL selection setsIrrelevant data
Retrieve just in timeDoes it need to be loaded now?IDs and references, tool search, file pathsData needed only later, or never
Clear and compactDoes it still need to be here?Tool result clearing, summaries, sub-agentsStale history
EncodeHow cheaply can the rest be represented?TOON for uniform arrays, compact JSON for deep nestingRepeated keys and punctuation

How do you select the right fields?

Start at the tool boundary. Anthropic's post says tools should promote efficiency "both by returning information that is token efficient and by encouraging efficient agent behaviors." Most APIs return far more than a model needs. A support-ticket endpoint might return this for every ticket:

{
  "id": 4812,
  "status": "open",
  "priority": "high",
  "customer": "Acme GmbH",
  "subject": "SSO login loop after IdP change",
  "created_at": "2026-09-28T08:14:02Z",
  "updated_at": "2026-09-30T16:41:55Z",
  "assignee_id": 77,
  "group_id": 3,
  "via": { "channel": "email", "source": { "from": "it@acme.example" } },
  "custom_fields": [{ "id": 360001, "value": null }, { "id": 360002, "value": "eu-west" }],
  "sla_policy": { "id": 9, "title": "Enterprise" },
  "url": "https://support.example.com/api/v2/tickets/4812.json"
}

For a triage step, the model needs five of those fields. Project them first, then encode. This is the actual output of @toon-format/toon 4.1.1 for three projected tickets:

tickets[3]{id,status,priority,customer,subject}:
  4812,open,high,Acme GmbH,SSO login loop after IdP change
  4815,open,medium,Northwind,CSV export drops last row
  4821,pending,high,Globex,Webhook retries exceed limit

Projection removed the nested via, custom_fields and sla_policy objects, which are exactly the parts that would have made the array non-uniform. As a side effect, the result became a flat table, which is the shape where TOON saves the most. In the benchmark's token-efficiency results, flat-only datasets came out 58.7% smaller than JSON, versus 32.7% for mixed structures. Selection and encoding reinforce each other.

What is just-in-time retrieval?

Instead of loading everything that might be relevant up front, the agent keeps "lightweight identifiers (file paths, stored queries, web links, etc.)" and uses them "to dynamically load data into context at runtime using tools," in Anthropic's words. Claude Code works this way: it reads project files with glob and grep when it needs them rather than indexing the whole repository into the prompt.

The same idea applies to tools themselves. Claude's tool search tool docs note that a typical multi-server setup (GitHub, Slack, Sentry, Grafana and Splunk) "can consume ~55k tokens in definitions before Claude does any work," and that tool search "typically reduces this by over 85 percent." No data format would get you an 85% cut on that. Not loading the definitions does.

Just-in-time retrieval does have a cost: extra tool calls and latency. For stable content, like a project's conventions file, loading up front is usually the better trade.

When should old tool results be cleared?

Usually as soon as the agent has acted on them. Anthropic calls tool result clearing "one of the safest lightest touch forms of compaction" and asks the obvious question: "once a tool has been called deep in the message history, why would the agent need to see the raw result again?"

Heavier options exist for long-horizon work. Compaction summarizes the conversation and restarts with the summary. Structured note-taking writes progress to memory outside the window. Sub-agents explore with their own clean context and return "a condensed, distilled summary of its work (often 1,000-2,000 tokens)."

Format interacts with each of these. A sub-agent that returns its findings as a TOON table instead of pretty JSON fits more rows inside that 1,000 to 2,000 token budget. A cleared result costs nothing, whatever its format. For techniques focused on fitting more into a fixed window, see context window management with TOON.

Which format should the remaining data use?

Choose by data shape, and keep the honest exceptions in mind:

  • Uniform arrays of objects (rows from SQL, search hits, tickets, logs): TOON. This is where the benchmark gains come from.
  • Deeply nested or irregular objects: compact JSON. On the benchmark's mixed-structure track, TOON was 1.6% larger than minified JSON in total. See TOON vs minified JSON.
  • Purely flat tables: CSV is slightly smaller. The TOON README puts TOON's overhead at about 5 to 10% over CSV, in exchange for explicit lengths and field headers.
  • Anything a program parses, such as tool inputs and structured outputs: JSON.

TOON's explicit structure also carries signal, not just savings. In the same benchmark, TOON scored 100% on structural-validation questions versus 50% for JSON, because the declared row count and field header let a model spot truncated or malformed data. For agent loops specifically, weigh the "Notation Matters" study, which found TOON "not safe as a default in multi-turn agentic systems" on open-weight models. Test before switching formats for agents. Our guide to when not to use TOON covers the edge cases.

A practical context-engineering checklist

  1. List every source of tokens in a typical request: system prompt, tool definitions, retrieved documents, tool results, history.
  2. Defer tools the current step does not need, using tool search or a smaller tool set per sub-agent.
  3. Project tool results down to the fields the step uses. Return IDs for everything else.
  4. Clear raw tool results once acted on, and summarize long histories.
  5. Encode what remains by shape: TOON for uniform arrays, compact JSON for nested data.
  6. Measure accuracy, not just token counts, on your own tasks.

Frequently Asked Questions

What is context engineering?

Anthropic defines it as the set of strategies for curating and maintaining the optimal set of tokens during LLM inference. It covers everything that lands in the context window, including system prompts, tool definitions, tool results, retrieved documents and message history, not just the instructions you write.

How is context engineering different from prompt engineering?

Prompt engineering is about writing and organizing instructions. Context engineering is about managing the whole token budget across many turns: which tools to load, which results to keep, what to retrieve and when, and how to represent data. In agents, most tokens come from tools and history, not the prompt.

Does a compact data format count as context engineering?

Yes, but it is the last step, not the first. Decide which records and fields the model actually needs, then pick a format. A compact format like TOON removes repeated keys and punctuation from uniform data, while selection removes irrelevant tokens entirely. Doing both beats doing either alone.

What is tool result clearing?

It means removing old raw tool outputs from the message history once the agent has used them. Anthropic calls it one of the safest, lightest-touch forms of compaction, because the agent rarely needs to re-read a raw result from many turns ago.

Recommended Reading

Context EngineeringAI AgentsTOONPrompt EngineeringLLMRAG