model: frontmatter in custom commands doesn't manage context for target model's window
Bug: model: frontmatter in custom commands doesn't manage context for target model's window
Description
When a custom slash command specifies model: haiku (or any smaller model) in its YAML frontmatter, Claude Code correctly switches to that model but does not manage the conversation context to fit the target model's context window. The full conversation history is passed to the smaller model, which immediately hits the context limit.
Reproduction
- Create a custom command at
.claude/commands/test-cmd.md:
``yaml``
---
description: Test command
model: haiku
---
# Test
Do something simple.
- Have a conversation with any amount of context (even moderate)
- Invoke
/test-cmd
- Observe:
````
● Skill(/test-cmd)
⎿ Successfully loaded skill · claude-haiku-4-5-20251001
⎿ Context limit reached · /compact or /clear to continue
Expected Behavior
When model: switches to a model with a smaller context window, Claude Code should automatically compact or truncate the conversation context to fit the target model's window — similar to how /compact works when context grows too large during normal operation.
Actual Behavior
The full conversation context is passed to the smaller model unchanged. The model immediately hits its context limit, making the command unusable. The user is forced to /compact or /clear before invoking the command, which defeats the purpose of model routing (using cheaper/faster models for simple operations).
Impact
This makes the model: frontmatter feature effectively unusable for any command that routes to a smaller model in a non-trivial session. The feature works correctly in a fresh session with minimal context, but fails in real-world usage where conversations accumulate context.
Environment
- Claude Code CLI
- Windows 11 (Git Bash), but likely affects all platforms
- Observed with
model: haikutargetingclaude-haiku-4-5-20251001
Suggested Fix
When loading a command with model: frontmatter that targets a model with a smaller context window than the current model:
- Calculate available context for the target model
- If current context exceeds target window, auto-compact before dispatching
- Alternatively, pass a truncated/summarized context to the target model
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗