Background agent completion causes instant context jump from 32% to 100% on 1M context window

Resolved 💬 3 comments Opened Mar 23, 2026 by aisidhe Closed Mar 27, 2026

Background agent completion causes instant context jump from 32% to 100% on 1M context window

Description

When a background agent (invoked via the Agent tool with run_in_background=true) completes and its output is folded back into the main conversation, the context usage jumps instantaneously from ~32% to 100%, killing the session. The jump is far larger than the agent's actual output would account for, and autocompact does not trigger to prevent hitting the limit.

Steps to reproduce

  1. Start a Claude Code session with Opus 4.6 (1M context)
  2. Work through a long session until context reaches ~32%
  3. Kick off a background agent (Agent tool, run_in_background=true) to perform a moderate task — in this case, reading several source files and writing a ~117KB markdown document
  4. Continue working (or wait) until the background agent completes

Expected behavior

  • The agent's output (~100K tokens at most) should add incrementally to the existing context (~320K tokens), bringing total to roughly ~420K tokens (~42%)
  • If context approached the limit, autocompact should trigger before reaching 100%

Actual behavior

  • Context jumped instantly from 32% to 100% upon agent completion
  • Agent returned with error: Not logged in · Please run /login
  • Immediately followed by: Context limit reached · /compact or /clear to continue
  • Session was effectively killed — no opportunity for autocompact to intervene

Analysis

The math does not add up:

| Component | Estimated tokens |
|---|---|
| Main conversation at 32% of 1M | ~320,000 |
| Agent output (~117KB document + file reads) | ~100,000 |
| Agent transcript overhead (81 messages: 19 user, 25 assistant, 37 progress) | variable |
| Expected total | ~420,000 (~42%) |
| Actual reported | ~1,000,000 (100%) |

Two things appear to be going wrong:

  1. Disproportionate context consumption: The background agent's transcript is being injected into context in a way that consumes far more tokens than the actual content warrants. Possibly the full internal agent transcript (all 81 messages including progress messages) is being appended verbatim rather than summarized or truncated.
  1. Autocompact bypass: The jump is instantaneous — context goes from 32% to 100% in a single step when the agent result is folded in. Autocompact never gets a chance to run because there is no gradual increase; the entire agent transcript lands at once.

The spurious "Not logged in" error also suggests something is going wrong in the agent completion pathway — possibly a token/auth issue triggered by the context overflow.

Environment

  • Claude Code version: latest as of 2026-03-23
  • Model: Opus 4.6 (1M context), confirmed by status bar
  • Platform: macOS (Darwin 25.3.0)

Impact

  • Session is killed with no recovery path other than /clear
  • No work was lost in this instance (the main task was already complete before spawning the agent), but this could easily cause loss of in-progress work
  • Background agents become risky to use in long sessions, defeating their purpose

Suggested fix

  • Background agent transcripts should be summarized/compacted before injection into the main context, or at minimum, progress messages should be stripped
  • Autocompact should be triggered before injecting agent results if the injection would exceed the context limit
  • The context accounting for background agents may need to be reviewed — the agent's context consumption during execution may be getting double-counted or the full transcript may be included when only the result should be

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗