← Reading Library / 返回总览
Highlights

X Article · Published 2026-07-16 · Saved 2026-07-20

Build self-managed agent system that uses 90% fewer tokens in 5 steps : subagents, memory, context

五步构建节省 90% Token 的自主管理智能体系统:子智能体、记忆与上下文

By codila · @0xCodila · Original source ↗

Your agents forgets what you told it ten minutes ago - and charges you more for the privilege

Every turn, it re-sends everything it's holding: the whole codebase, the whole doc, the whole history You pay for all of it, again, on every single message, then it forgets anyway

I put together 4 steps that fix the memory and token-spend problem for good and +1 step that will create self managed agent system for you before the alpha - subscribe to my substack for more fresh alpha ↓ https://substack.com/@0xcodila

Step 1 - Get the context out of the window

Image

The instinct is to load everything up front so the model "has what it needs" That's the trap - everything you load is something you pay for on every turn until the conversation ends

  • Put the big stuff in a file instead - the book, the codebase, the docs, the logs Then let the agent reach into that file only when it actually needs a piece of it - It can grep for a keyword, read one section, and move on

The 108,000-token book sits on disk. The question costs you the question.

→ This one move is where most of the savings come from. The rest of the steps are about not undoing it How Claude Code uses prompt caching

Step 2 - Send the messy work to a subagent

Image

Some jobs generate a wall of tokens you don't want in your main thread - running a test suite, reading a stack trace, crawling a doc site. - All of that noise lands in your context window and stays there, costing you on every following turn.

- Hand those jobs to a subagent It does the dirty work in its own separate window, burns through the noise there, and hands you back a clean summary. Your main thread never sees the 4,000 lines of test output - it sees "three tests failed, here's why"

→ Think of the subagent as a filter- everything loud goes in, one quiet answer comes out Subagents build their own cache and context

Step 3 - memory self-managed agent

Image

(the crazy part) Your agent starts every session from zero. You re-explain the same context, and you pay tokens to reload it Claude Code fixes this natively -and most people don't know it's already on

Auto Memory means Claude writes its own notes between sessions - build commands, bug fixes, the workarounds you found - and loads them at the start of the next one you don't write anything - It decides what's worth keeping

Do this today:

→ Run /memory to see what Claude has already recorded on its own. You'll usually find it's captured conventions and fixes you never documented.

→ Delete those lines from your CLAUDE.md. Anything Claude learns after one session doesn't belong in the file you pay for on every turn - the memory holds it for free.

→ For bigger projects, make MEMORY.md a table of contents, not a dump One line per topic file (gotchas.md, conventions.md), so Claude pulls in only the file the current task needs - not all 200 lines.

  • That last move is the whole game in miniature: the agent carries its knowledge forward, but only the slice it needs enters the prefix Small prefix, small bill, and you stopped re-explaining yourself.

→ It's already running. Your only job is to stop duplicating in CLAUDE.md what memory already holds.

Source: Claude Code auto memory docs and Anthropic - Agent memory & "dreaming

Step 4 - Stop breaking the cache

Image

(my favorite step) Here's the part most people don't know is running Everything stable at the front of your prompt - tools, system prompt, that tidy CLAUDE.md - gets cached after the first turn. A cached token costs a tenth of a fresh one

The catch: the cache matches an exact prefix. Change one character before the cache point and everything after it goes back to full price Silently. No error.

The usual culprits:

  • A timestamp or date baked into the system prompt - changes constantly, breaks the cache every time.
  • Switching models mid-session - each model keeps its own cache, so a /model swap re-reads the whole thing from scratch.
  • A stray space, a reordered tool list, or an MCP tool added halfway through.

The rule is simple, static stuff at the top, in a fixed order. Anything that changes - the date, the user's turn, session junk - goes at the bottom, below the cache point

Anthropic treats this as serious enough that internal teams declare an incident when their hit rate drops Anthropic prompt caching docs GitHub keeps theirs above 94% and calls a drop to 70% a bug. Their CPO put it well - it's like high-frequency trading, where 1% of efficiency is millions of dollars

→ Most people are sitting at 40% and have no idea

Step 5 - Manage the context by hand

Image

Claude Code will compact your context on its own when it fills up The problem is it does this on its schedule, not yours, and compaction can quietly eat most of your remaining tokens at the worst moment

Two commands give you the wheel back:

  • - shows you what's actually taking up space right now - you'll usually find something bloated you forgot about.
  • - lets you squash the conversation at a clean break, like when you finish one task and start another, instead of waiting for the automatic version to trigger mid-thought.

→ The habit is small, Check /context when a session feels heavy. Compact between tasks, not in the middle of one

Part 6 · Honestly

The five steps above genuinely cut your bill - you just learned the thing most people never touch - and that alone puts you ahead of them

One thing worth saying out loud, so this keeps working

None of it is set-and-forget. Caching lives on a number you only see if you go look - your hit rate, your cache_read_input_tokens. Setting it up once is great - Glance at it once a week, and a stray timestamp in your prefix won't quietly steal the savings back.

A cheap agent is a state you hold, and holding it is easy now, because you know where to look

Here's the good news: you're in good company.

Andrew Ng and Anthropic arrived at the same place from the other side - Ng teaches you to move an agent's memory out of the window - that's step one, just from the memory angle - Anthropic went further: an agent that cleans its own memory in the background, so the prefix stays small on its own

Three different doors, one room - stop carrying everything, start carrying what the task needs - you just walked in

The tools will change: cache, subagents, memory stores - half these names will be different in six months

One thing won't: the engineer who knows what their agent is holding, and why, always beats the one who dumped everything into the window and hoped

Now you're the first one, so run your agent like an engineer. @0xCodila