TIL: Claude Code's /context command works in non-interactive mode
This is useful when coding with Ralph loops
My team has been doing a lot of coding with Ralph loops lately. This means running Claude Code in headless mode with no human involvement.
while true; do
cat prompt.md | claude -p --dangerously-skip-permissionsThe prompt tells Claude to look up the next feature from a task list and build it. But a challenge is that bigger features eat more tokens, and therefore need bigger context windows. We try to keep the context window <50% to avoid rot so we want to get better at writing feature PRDs of consistent sizes.
Measuring this in an interactive Claude Code session is easy, just run /context. But it wasn’t clear how to do that in non-interactive mode and get a categorical breakdown of tokens.
It turns out the command can be invoked in ralph loops but there are a couple of catches:
For the categorical breakdown of tokens, you need to include the --verbose and --output-format json params
If we end a prompt with something like “run your context slash command” or “run /context” or even just “/context”, CC doesn’t always execute it. So we add a second call to run CC but continue the session
Simple example:
#!/bin/bash
counter=0
while [ $counter -lt 1 ]; do
echo “Write a 10 line poem about snow, then make an html file that displays the poem. When a user mouses over a word, it shows the word spelled backwards in a little tooltip popup. Use your frontend-designer skill to make the html look like something out of the game pacman. Be sure to make the page scrollable” | claude -p --dangerously-skip-permissions
echo “/context” | claude -p -c --output-format json --verbose | \
jq -r ‘.[1].message.content’ | \
sed ‘s/<local-command-stdout>//’ | sed ‘s/<\/local-command-stdout>//’ \
> context_log.txt
((counter++))
doneNow we have context data on each pass:
johndamask@Johns-MacBook-Pro-2 2026-01-31-context-testing % more context_log.txt
## Context Usage
**Model:** claude-sonnet-4-5-20250929
**Tokens:** 31.4k / 200.0k (16%)
### Estimated usage by category
| Category | Tokens | Percentage |
|----------|--------|------------|
| System prompt | 2.9k | 1.5% |
| System tools | 14.3k | 7.2% |
| Custom agents | 2.4k | 1.2% |
| Memory files | 466 | 0.2% |
| Skills | 3.8k | 1.9% |
| Messages | 7.2k | 3.6% |
| Free space | 135.9k | 68.0% |
| Autocompact buffer | 33.0k | 16.5% |
### Custom Agents
| Agent Type | Source | Tokens |
|------------|--------|--------|
| john-skills:session-recap | Plugin | 87 |
| agent-sdk-dev:agent-sdk-verifier-ts | Plugin | 72 |
| agent-sdk-dev:agent-sdk-verifier-py | Plugin | 70 |
| feature-dev:code-reviewer | Plugin | 52 |
| feature-dev:code-explorer | Plugin | 47 |
| feature-dev:code-architect | Plugin | 53 |
| pr-review-toolkit:code-reviewer | Plugin | 466 |
| pr-review-toolkit:silent-failure-hunter | Plugin | 361 |
| pr-review-toolkit:code-simplifier | Plugin | 25 |
| pr-review-toolkit:comment-analyzer | Plugin | 438 |
| pr-review-toolkit:pr-test-analyzer | Plugin | 385 |
| pr-review-toolkit:type-design-analyzer | Plugin | 342 |
### Memory Files
| Type | Path | Tokens |
|------|------|--------|
| User | /Users/johndamask/.claude/CLAUDE.md | 466 |
### Skills
| Skill | Source | Tokens |
|-------|--------|--------|
| keybindings-help | undefined | 61 |
| prd | User | 63 |
| explain-code | User | 42 |
| agent-sdk-dev:new-sdk-app | Plugin | 19 |
| code-review:code-review | Plugin | 13 |
| commit-commands:commit-push-pr | Plugin | 15 |
| commit-commands:clean_gone | Plugin | 46 |
| commit-commands:commit | Plugin | 11 |
| feature-dev:feature-dev | Plugin | 25 |
| pr-review-toolkit:review-pr | Plugin | 19 |
| devlog | Plugin | 106 |
| ec2-claude-code | Plugin | 107 |
| docetl-claude | Plugin | 95 |
| repo-security-review | Plugin | 107 |
| venv-manager | Plugin | 78 |
| plugin-marketplace-creator | Plugin | 123 |
| mcp-scanner | Plugin | 92 |
| gitingest | Plugin | 72 |
| frontend-design | Plugin | 67 |
| theme-factory | Plugin | 73 |
| doc-coauthoring | Plugin | 115 |
| xlsx | Plugin | 116 |
| pdf | Plugin | 68 |
| algorithmic-art | Plugin | 89 |
| internal-comms | Plugin | 90 |
| skill-creator | Plugin | 64 |
| canvas-design | Plugin | 80 |
| pptx | Plugin | 73 |
| slack-gif-creator | Plugin | 65 |
| webapp-testing | Plugin | 59 |
| frontend-design | Plugin | 108 |
| mcp-builder | Plugin | 76 |
| brand-guidelines | Plugin | 67 |
| docx | Plugin | 96 |
| web-artifacts-builder | Plugin | 81 |
| theme-factory | Plugin | 73 |
| doc-coauthoring | Plugin | 115 |
| xlsx | Plugin | 117 |
| pdf | Plugin | 68 |
| algorithmic-art | Plugin | 89 |
| internal-comms | Plugin | 90 |
| skill-creator | Plugin | 64 |
| canvas-design | Plugin | 80 |
| pptx | Plugin | 73 |
| slack-gif-creator | Plugin | 65 |
| webapp-testing | Plugin | 59 |
| frontend-design | Plugin | 108 |
| mcp-builder | Plugin | 76 |
| brand-guidelines | Plugin | 67 |
| docx | Plugin | 96 |
| web-artifacts-builder | Plugin | 82 |

