Preventing Unnecessary Token Consumption through Agent Isolation
In legacy projects, all logic is tightly coupled. Agents like Claude Fable 5 often scan dozens of unrelated files even for a single modification. This indiscriminate scanning leads to a surge in costs. Just as Stripe protected against malfunctions during migration by isolating core logic modules instead of processing the entire source, we need physical boundaries.
Create temporary session spaces to strictly limit the agent's access scope.
- Create a git worktree: Run the agent in a directory separated from the main branch to prevent contamination of the original code.
- Restrict access rights: Use the
denyWriteproperty in the.claude/settings.local.jsonfile to prevent the agent from accessing core engines, private keys, or authentication assets like.env. - Deploy CLAUDE.md: Place a guide file in each directory for the agent to reference, allowing you to control the context.
This approach enables one-click rollbacks even if the agent makes a mistake and reduces token consumption by narrowing the unnecessary scan range.
Building a Test Automation Pipeline
It is a waste of time for developers to manually run tests in the terminal and relay results to the agent. Automate this process using Git Hooks. Configure the agent to run tests only on changed files and cache the results when a commit is attempted.
Insert the following logic into the .git/hooks/pre-commit file:
- Detect changes: Target only modified files using
git diff --name-only. - Integrate testing: Execute
npm test -- --findRelatedTeststo perform only the tests related to the changed code and save the results to.claude/diagnostics/local_test_run.log. - Autonomous recovery: When a test fails, call
claude -pwith the--allowedToolsand--dangerously-skip-permissionsflags.
By using this pipeline, developers commit only integrity-verified build assets without manual validation, saving at least 5 hours of debugging time per week.
Token Usage Control and Budget Management
Claude Fable 5 API costs are $10 per 1 million input tokens and $50 for output. The budget can be depleted quickly during long-term execution. Control costs with a script that blocks unnecessary file scanning.
Minimizing in this equation is the key. Create a bin/optimize-fable-context.sh script to perform the following:
- Dynamic filtering: Detect recently modified files via
git diffand assign them toACTIVE_DIR, while dynamically adding other paths to.claudeignoreto prevent the agent from reading them. - Leverage caching: Apply a 90% discount on repetitive calls using the prompt caching feature.
- Routing: Delegate simple analysis to Sonnet or Haiku, and use Fable 5 only for high-risk core logic modifications.
Applying this method can reduce token consumption caused by file exploration by an average of 40%.
Automatically Log Work to Management Tools
If the record of collaboration with the agent disappears when a session ends, only management debt remains. Build a pipeline that automatically records work details to Notion or Jira.
- Normalization: Use the
--json-schemaflag when running Fable 5 to ensure output is delivered in a fixed JSON format. - API Integration: Create
bin/publish_agent_report.pyusing the Pythonrequestslibrary. - Documentation: Map the code review results to the Markdown field of the Notion API or the Jira Wiki Markup format and send them.
Records must remain centralized so there is no confusion when other developers take over the project later. Use // [AI-GEN-START] and // [AI-GEN-END] tags at the beginning and end of code blocks to specify areas written by AI. For ambiguous code, leave TODO(human) comments to ensure human review, preventing the decay of legacy systems.
