Mhonn YbañezSRE, DevOps, AI Engineer
 Today I Learned
Read the full page

Does a smaller context make an AI agent better?

Posted
Tags
ai, llm, agents

I built a LangGraph agent that solves detective mysteries while keeping its context window as small as possible, then ran it about 340 times, on one model locally and four in the cloud. Keeping the context small worked, but it was the wrong thing to optimize: sending the whole case in one prompt was more accurate and 7x to 20x cheaper. What goes in the prompt matters more than how little goes in.

Yesterday I ended my LangGraph post with a mini project: LangGraph Detective, an agent that solves a detective mystery while keeping its context window as small as possible. Today I finished it, and the result was not what I expected.

The question I had

If an agent only carries short notes instead of everything it has read, does it do better? Or at least, does it do as well for less?

What I built

The code is on GitHub: mhonnczedz1/langgraph-detective.

The task

Each mystery has a short brief, a folder of clue files (witness statements, logs, receipts), and a hidden solution only the scorer can read. The agent reads clues with a tool and then calls an Accuse tool with the culprit, the method, the motive, and the clues that prove it. A run passes only if it names the right culprit, cites at least half of the key clues, and does it within a turn limit.

I wrote six cases, from 8 clues (about 3,000 tokens) to 40 clues (about 14,000 tokens). The biggest one is padded with distractors: delivery rotas, tide tables, meeting minutes, some of them with times and names close to the real ones.

The harness

The harness is a LangGraph loop. Each turn the model sees only a small set of memory files: the goal, a summary of what it knows, and an index of which file holds which details. After every clue it reads, a second model call folds the clue into the notes and the raw text is dropped. So the prompt stays small no matter how many clues it has read.

I later added two more layers: a "focus" (the one question the agent is working on right now) and a short log of its recent turns.

What I compared it against

  • One prompt: the whole case, every clue, in a single call.
  • Plain chat: every clue stays in the conversation, which grows each turn.
  • Budget mode: clue text stays word for word as long as the prompt fits in 65% of the window, and only gets compressed when it has to.

The models

First Qwen 3.5 9B on my laptop, then four models on OpenRouter: Qwen3.5 9B and 27B, DeepSeek V4.1 Flash, and DeepSeek V4 Pro.

What I found

One prompt beat the harness

Averages per run over all six cases, one prompt against the memory harness:

ModelMethodPassedLargest promptTotal tokens readCost per run
Qwen3.5 9BOne prompt11/1810,50814,549$0.0018
Harness8/188,769104,423$0.0131
Qwen3.5 27BOne prompt16/188,2278,227$0.0028
Harness15/184,775123,363$0.0546
DeepSeek V4.1 FlashOne prompt18/187,5217,521$0.0004
Harness18/184,462139,470$0.0070
DeepSeek V4 Pro (partial)One prompt12/124,9784,978$0.0024
Harness4/42,45336,775$0.0162

DeepSeek V4 Pro only finished 25 runs before my credit ran out, mostly on the smaller cases. In total I ran 332 cloud runs for $4.22.

The harness shrank the wrong number

The harness did what I built it to do: each prompt was 40% to 50% smaller. But it read 7x to 18x more tokens in total, because it re-sends its notes every turn and makes two calls per turn. A paid API bills the total, not the largest prompt. And every case fit easily in every cloud model's window (262,000 tokens for the Qwens, about a million for DeepSeek), so the smaller prompts bought me nothing.

Summaries were where the errors came from

Locally, the 9B kept failing through its own notes. One clue had a log of who used a keycard that night, and the notes turned it into "no staff entered the building", which then stayed in the notes for good. In other runs the agent marked the real culprit as "cleared" by reasoning backwards from a correct fact. Given the whole case in one prompt, the same model solved every original case, including the hardest. It could reason. My harness lost the information.

The model mattered more than my memory design

DeepSeek V4.1 Flash passed every cloud run that could finish, 75 of 75, in every approach I tried, and it was the cheapest. (Its other 27 runs hit a budget step I never built, more on that below.) The 9B was the opposite: on the new harbour case it failed the small size 0 of 3 even with everything in one prompt. And the extra layers only hurt the weak model: on one case the 9B went from 3 of 3 with the basic harness to 0 of 3 with the focus added, while Flash stayed at 3 of 3.

There was one exception worth keeping. On the biggest case, 40 clues with 28 distractors, the 9B's harness passed 3 of 3, while one prompt and plain chat failed all six of their runs. For a weak model facing a lot of noise, taking the case one clue at a time helped. So the harness isn't useless. It just wasn't worth it for the strong models, or for cases that fit comfortably.

Where my epiphany came in

What I put in the prompt matters

The biggest lesson for me: what goes in the prompt matters more than how little goes in. If I'm using AI on a big system, like a large codebase, and the context runs out, I shouldn't summarize it. I should focus on the specific parts the task needs and give those in full. In the harness, summaries were where the errors came from, and original text worked.

Don't optimize for the wrong thing

I set out to make the context fill up as slowly as possible, and I did it. It just wasn't the thing that limited the task. Accuracy, cost, and speed were, and the simplest method won on all three. Each clever layer I added made the weak model worse and gave me new bugs to fix. Next time I'll measure what actually limits the task first, start with the simplest thing that could work, and add structure only when a number tells me to.

A harness wraps the model in deterministic structure

I used to think of harness engineering as making AI output consistent, like deterministic code. That's not quite reachable: even with the temperature at 0 and a fixed seed, my cloud runs varied. What a harness can do is make the outcome reliable around a model that isn't. It constrains what the model can produce (tools, fixed label lists, JSON schemas), checks the output in code, caps time, length and cost, and keeps every exact rule out of the prompt. In my project, file access, the context budget, the note layout, and the scoring are all plain code. The model only does the judgment.

Takeaway

Put the right material in the prompt, and send it whole when it fits. Don't build machinery for a problem you don't have: measure what actually limits the task first, and keep anything that must be exact in code.