Hacker News new | ask | show | jobs
Show HN: Context Surgeon – Let AI agents edit their own context window (github.com)
5 points by jdjdjdi 64 days ago
AI agents accumulate stale tool results — file reads, web fetches, bash outputs — in their context window. Every one sits there for the entire conversation, consuming tokens and degrading quality. The standard fix is auto-compaction: wait until full, then drop content indiscriminately.

Context Surgeon gives the agent three operations — evict, replace, and restore — so it can manage its own context. It works as a transparent local proxy that intercepts API requests, assigns IDs to content blocks, and applies eviction directives before forwarding. The agent calls the tools via bash. The proxy is ephemeral: starts with your CLI session, dies when it ends.

The interesting part is that agents naturally start managing their own context without much prompting. In testing, the agent decided on its own to evict old research results before starting an implementation phase.

Works with Codex CLI and Claude Code.

Install: npm install -g context-surgeon Usage: context-surgeon codex

2 comments

Really interesting project. One thing: why not just save the eviction state to a file automatically when the session ends, and just load it the next time you run context-surgeon in the same project dir? Could context-surgeon do the same as git with the .git folder example, such as with a .surgeon folder? Seems like it would make the tool much more useful for multi-session work.
Good call, next on my todo list, wasn’t a priority for the mvp, but got multiple requests for state persistence
Nice! Glad it’s making the list.
I like the approach to bring in the context surgeon as a wrapper that sees the conversation and can attach ids to the lines. You might even use the surgeon to replace parts with summaries?
Yes! The replace command lets the agent write their own summary for any tool result or message or a single summary referring to several turns