{"slug": "one-card-one-context-sat-in-the-docs-for-two-weeks-then-one-day-cost-420m-tokens", "title": "\"One card, one context\" sat in the docs for two weeks. Then one day cost ~420M tokens, and the guard got bypassed the day after", "summary": "A developer building a Claude Code and Codex task-card workflow described how a \"one card, one fresh context\" rule written only into instructions went unenforced for two weeks, then cost roughly 420M tokens in a single day when two subagents dispatched and accepted 26 and 8 cards inside single contexts. A first guard placed inside the dispatch script was bypassed within a day because it checked only thread creation, not the turn command that drives a thread, costing another ~132M tokens; the fix routes both start and turn through the same guard and identifies the calling context by finding the one JSONL transcript with an unresolved Bash call. The developer reported the guard still missed two start calls on November 17 and added a non-blocking hint hook that nudges the model toward a fresh session at 200K tokens of context and every 100K after.", "body_md": "The short version: **a rule that limits how long an agent's context gets cannot live at the \"entrance\". It has to live inside the command the agent cannot avoid, and every action that moves work forward has to check it.** I wrote \"one task card, one fresh context\" into my instructions and nothing enforced it for two weeks. The first time I turned it into code, it was bypassed within a day.\n\nSome background. I use Claude Code to split work into task cards and review the results, and Codex to implement them. Dispatching, monitoring and accepting a card is supposed to happen in a short context opened for that card alone.\n\nThe rule dates from a review on October 30. Every request resends the whole conversation, and back then I ran one long session per project. One transcript had grown to 32 MB with 1,878 tool results. By the later cards, every turn was re-reading the history of all the earlier ones.\n\nSo the rule went into the dispatch skill, as its first structural rule. Nothing else changed.\n\nOn November 14, a batch of cards was due in a data platform repository. Two subagents each received a batch: one dispatched and accepted 26 cards inside a single context, the other 8.\n\nThat day:\n\nThe side doing planning and review spent more than 8 times what the side doing implementation spent.\n\nTwo things let it happen:\n\n`dispatch.sh` for convenience, wrapping the dispatch command. A hook only saw \"a shell script ran\", not each real dispatch call inside it. Even a hook aimed at the dispatch command would have been blind.\nThe same day the rule became code. Not in a hook, but inside the dispatch script itself: before creating a thread, `codex_thread.py start` calls `dispatch_scope.py`, which asks whether this context has already dispatched a different card. If it has, the call is refused. A second round on the same card passes.\n\nOn November 15 it was bypassed.\n\nA subagent had 14 cards to handle. It let throwaway subagents run only `start`, then stayed in its own context and ran `turn` (which drives the thread) and acceptance for all 14 cards. The guard checked `start`. It did not check `turn`. That cost about 132M tokens, again re-reading one growing context.\n\nThe guard literally said \"`start` may not cross cards\", and this path obeyed it to the letter. **Whatever action you guard, the work moves to the one you did not.**\n\nThe fix landed the same day: `turn` calls the same guard, and every context that drives a thread is written into the thread's registry file. Once a context has touched one card, through `start` or `turn`, it cannot touch another.\n\nThe hard part: when the script runs, which context ran it? A main session and its subagents share one session ID.\n\nThe answer is in the transcripts. Claude Code keeps one JSONL transcript for the main session and one per subagent. At the moment the guard runs, exactly one of them has a Bash tool call with no result yet, because the guard is that call. So:\n\n`CLAUDE_CODE_SESSION_ID` environment variable;\nWhenever the caller cannot be identified, the guard lets the call through: when I run the script myself from a terminal, or when two contexts are waiting on Bash at the same instant. **The guard would rather miss than block a person by mistake.**\n\nSince the guard went in, registry files carry a caller field. As of the morning of November 18:\n\n`start` for 2 cards on November 17, and the guard did not stop it. I have not found out why yet, so it stays on the record as an open leak.\nI also added a hint hook that never blocks, `long_session_hint.py`, on the prompt-submit event. When the main conversation reaches 200K tokens of context, and at each further 100K, it tells the model to hand new results to a fresh session and tells me the current size. On any error it exits quietly. The reason: on November 14 and 15, main sessions kept working at 220K to 490K tokens of context, while Codex threads in the same window had a median context of 67K.\n\nThe core is under 20 lines. This is a simplified version. The registry holds one JSON file per thread, with the card path and the contexts that touched it:\n\n``` python\ndef check(threads_dir, card, who):\n    \"\"\"Return a refusal message if `who` already touched another card, else None.\"\"\"\n    if not who:\n        return None  # caller unknown: let it through\n    card = os.path.realpath(card)\n    for path in glob.glob(os.path.join(threads_dir, '*.json')):\n        with open(path, encoding='utf-8') as fh:\n            reg = json.load(fh)\n        touched = reg.get('caller') == who or who in reg.get('turn_callers', [])\n        if touched and os.path.realpath(reg['card']) != card:\n            return f'this context already touched {reg[\"card\"]}; open a fresh one for the new card'\n    return None\n```\n\nThree things to keep in mind:\n\n`turn`, and it cost 132M tokens.\nA quick test for whether a rule is actually enforced: **\"if the agent never read this sentence, what would happen?\"** If the answer is \"nothing\", the rule is still a wish.\n\nThis is part 6 of a series on running two coding agents together. Part 1 found that the manager costs more than the engineer. This one is the follow-up: once you know where the cost is, how do you make the rule hold.\n\n`dispatch_scope.py` and the rule text in the dispatch skill. They describe 2026-11-14. Most of the Claude Code side is cache reads, so this is token volume, not a bill. \"More than 8 times\" is 420M divided by 49M. I did not recompute these figures for this article.`check_turn` comment in the same script and the skill text, for 2026-11-15.`long_session_hint.py`, dated 2026-11-15.", "url": "https://wpnews.pro/news/one-card-one-context-sat-in-the-docs-for-two-weeks-then-one-day-cost-420m-tokens", "canonical_source": "https://dev.to/tomxiaoyz/one-card-one-context-sat-in-the-docs-for-two-weeks-then-one-day-cost-420m-tokens-and-the-35d9", "published_at": "2026-11-18 15:09:24+00:00", "updated_at": "2026-11-18 15:14:18.679268+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "large-language-models", "developer-tools"], "entities": ["Claude Code", "Codex", "Anthropic", "OpenAI"], "also_reported_by": [], "takeaway": "Guard every action that advances work, not just the entry point: a context-scope check on thread creation alone was bypassed via the turn command for ~132M tokens, so both start and turn now consult the same guard and record the caller in the thread registry.", "recommendation": {"advice": "Enforce context-per-task limits inside every command that advances work, not just the entry point, and identify the calling context from the transcript with an unresolved tool call.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/one-card-one-context-sat-in-the-docs-for-two-weeks-then-one-day-cost-420m-tokens", "markdown": "https://wpnews.pro/news/one-card-one-context-sat-in-the-docs-for-two-weeks-then-one-day-cost-420m-tokens.md", "text": "https://wpnews.pro/news/one-card-one-context-sat-in-the-docs-for-two-weeks-then-one-day-cost-420m-tokens.txt", "jsonld": "https://wpnews.pro/news/one-card-one-context-sat-in-the-docs-for-two-weeks-then-one-day-cost-420m-tokens.jsonld"}}