Back to blog
NO.
033
DATE
READ
~6 min
KIND
Notes
STATUS
Reviewed

TAGS: AI collaboration

Why I Stopped Letting Agents Resume Each Other

Pi resumed a Claude Code session and used nearly 20M tokens over 4 messages, so I switched agent handoffs to Git, a HANDOFF file and a fresh session.

While using Pi for a development task recently, I ran into a cost problem: a session that looked cheap, with a cache hit rate above 90%, still burned through nearly twenty million tokens. The root cause was not an expensive model or a cache that failed to work; it was that I had one agent take over another agent's unfinished session. This note records what was wrong with that structure and what I changed the handoff process to. It is my own cost-control experience, not a model limitation or a general conclusion.

Part of the task had already been done in Claude Code, so I naturally gave Pi one instruction: finish this unfinished session, claude --resume <session-id>. From a human point of view that is reasonable: Claude Code already knows the background, so resuming the session lets Pi supervise it to the finish without re-learning the project.

After it had been running for a while I glanced at API usage, and something looked off. This Pi session had only 4 user messages, yet produced:

Assistant: 53
Tool calls: 52

Input: 19.86M tokens
Cached: 18.02M
Cache hit: 90.7%

The cache hit rate was actually healthy. The problem was not whether the cache worked, but that the agent ran many consecutive turns on a context close to 1M tokens. Most of it hit the cache, but cached tokens are still not free. That made me rethink a question: how should a coding agent's unfinished task be handed to another agent?

The problem is not resume, it is agent inside agent

claude --resume itself is fine. If Claude Code is interrupted by a network or terminal problem, running claude --resume <session> to carry on is perfectly reasonable. The problem was the structure I had actually built:

Pi
 └── shell
      └── Claude Code
           └── Model API

Pi is an agent, and Claude Code is another complete agent. So what I had done was not let Pi "inherit Claude Code's task"; I had one agent launch another complete agent runtime. And Claude Code's terminal output, tool results and history kept flowing into Pi's context.

The growth in context size this time was blunt. It went from a few tens of K up fast, and after that most model calls stayed close to 1M:

~35K  →  ~490K  →  ~1.02M

What is really expensive is not one especially long answer; it is a context of nearly 1M tokens multiplied by many agent loops. Every time the model calls a tool and comes back to keep reasoning, the current context is processed again; a few dozen tool calls easily add up to tens of millions of tokens processed.

Where my old understanding went wrong

Without quite noticing, I used to treat "the session" as roughly equal to "the project state," thinking that restoring the session would let an agent pick up seamlessly. For a code project, that is not a good model of state. The reliable project state already lives in Git, the source code, configuration, tests and build results; the only extra things a chat history keeps are "why it was done this way" and "what is planned next," and that part does not need hundreds of thousands of tokens of conversation history.

So a coding agent's session should be treated as a temporary compute environment, not as the project database. What should be persisted is the repo.

What I use now

The principle is simple: one task keeps one main agent, and different agents hand over through files, not through sessions. Suppose Claude Code is halfway through and Pi needs to take over. I no longer have Pi run claude --resume directly; instead there are two steps.

Step one: if the Claude Code session can still be resumed, I go in first, but this time I don't let it continue developing. I give it only a wrap-up task:

Stop development. Write the information needed to continue this task into .ai/HANDOFF.md:

- Final goal
- Work completed
- Work not yet done
- Important files changed
- Design decisions already made
- Current test status
- Known problems
- Recommended next step
- How to verify the final result

Do not copy the full chat history, full source code or large logs.
Make sure the current changes are saved in the workspace, then exit.

Step two: Pi opens a brand-new session to take over:

Take over the current task. First read .ai/HANDOFF.md, git status and git diff --stat,
then read only the files needed to finish the remaining work.

First check whether HANDOFF matches the current code; if they conflict, Git and the actual code win.
Once confirmed, finish the remaining work.

The structure then changes from "Pi → Claude Code → old session → more output → fed back into Pi's context" to:

Claude Code  →  HANDOFF.md  →  Git / Workspace  →  new Pi session

What actually passes between the two agents is only the task state, not the whole history of reasoning.

HANDOFF doesn't need to be complicated

I now keep one .ai/HANDOFF.md in each project, and keep it short:

# Handoff

## Goal
What ultimately needs to be built.

## Completed
What is already done.

## Remaining
- [ ] Remaining task A
- [ ] Remaining task B

## Changed Files
- `src/a.ts`
- `src/b.ts`

## Decisions
Important design decisions already made, and why.

## Tests
Which tests have been run and what the results are.

## Known Problems
What problems remain.

## Next Step
What the next agent should do first.

## Verification
How to verify the task once it is done.

If an agent cannot say where it got to in a file like this, that often means the task itself has not been structured well yet.

Sessions need to be ended on purpose too

Another lesson this time: don't wait until the context is actually full before thinking about switching sessions. Pi shows the current context usage directly, and I now use a rough rule:

< 50%       carry on normally
50% ~ 70%   carry on if the task is nearly done; prepare a HANDOFF if a new phase is about to start
> 70%       don't start a large new task; wrap up the current step and switch sessions

This is not a technical limit of the model, just my own cost-control line. In a coding agent setting, one user instruction can trigger dozens of tool calls. Starting "while you're at it, refactor this module" at 70% context means the next few dozen model requests all carry a huge history with them, whereas opening a new session usually costs much less.

Switch models at session boundaries where possible

Another easily overlooked point is the cache. This time I switched models while the context was long, and once a context of nearly a million tokens had to have its cache rebuilt. So I now also try not to switch from Opus to another model in the middle of a 900K context and carry on. Instead I finish this phase in the current session, write the HANDOFF, open a new session and then switch models, so that the model switch and the context reset happen at the same boundary. The logic is cleaner that way too.

In the end I kept only four rules

After all that, I don't think a coding agent workflow needs to be elaborate. For now I plan to stick to four rules for the long term:

  1. The repo is the project state; a session is only temporary context.
  2. One task keeps one main agent; agents inside agents are not the default.
  3. Different agents hand over through HANDOFF.md + Git, not by resuming each other's full session.
  4. Don't force a long context; switch sessions on purpose when a new phase starts.

Cache hit rates, model prices and how large the context window is certainly deserve attention, but they are more like a second layer of optimization. What really matters in the first layer is not letting the project's continuity depend on one AI's chat history.

These days I judge whether a coding agent workflow is healthy by asking myself one question: if every Pi, Claude Code and Codex session were deleted right now, could a new agent carry on using only Git and a few state files in the project? If it could, the project state really belongs to the project, and the agents are just there to work for a while.

Writing note: Claude Opus 5.5 translated it from the Chinese original; the author decided to publish.

Comments →

CC BY-NC-SA 4.0

This post is licensed under CC BY-NC-SA 4.0 .

Did this article help you? ☕ Support my work

Comments

Comments are powered by GitHub Discussions. Sign in with GitHub to comment. Open the matching Discussion