Skip to content

Skip ctx_commands injection in subagent sessions #233

@movax01h

Description

@movax01h

Problem

The SessionStart hook injects the full <context_window_protection> block into every Claude Code session, including subagents spawned via the Agent tool.

The <ctx_commands> section (~400 tokens) contains instructions for user-facing commands (ctx doctor, ctx purge, ctx upgrade, ctx stats) that are useless in subagent contexts — users cannot type these commands in a subagent session.

This wastes ~400 tokens per subagent spawn. In workflows that use multiple agents (e.g., plan-mode with Explore + Plan agents), this adds up quickly.

Where

  • hooks/routing-block.mjs, lines 64-78 — the <ctx_commands> section
  • hooks/sessionstart.mjs, line 35 — additionalContext = ROUTING_BLOCK (always the full block, no conditional logic)

Suggested fix

  1. Detect subagent context — either via an environment variable (e.g., CLAUDE_PARENT_SESSION_ID) or a field in the stdin JSON that Claude Code passes to hooks
  2. When in a subagent session, omit the <ctx_commands> section from the routing block (or omit the entire routing block if MCP tools aren't available to subagents anyway)

A simple approach would be to split createRoutingBlock() into core guidance + user commands, and only include the user commands section for root sessions.

Environment

  • context-mode version: 1.0.75
  • Claude Code CLI

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions