The era of editors simply writing code on your behalf is over. Now, agents like Claude Code directly open terminals, traverse file systems, and modify code. While convenient, it's also somewhat chilling. A single prompt injection could potentially leak your .env files or SSH keys. We need specific control methods to give agents autonomy without letting them cross the line.
Writing a Constitution for Agents in the Project Root
By default, agents try to scan the entire project to perform a command. If left unchecked, there's a risk they might read sensitive configuration values. The most reliable defense is to create an agent-specific regulation file in the project root directory.
- Create a
CLAUDE.mdor.agent-rulesfile in the root. - Specify restricted paths within the file. You must hard-code
denyrules so the agent cannot even look at paths like.env,~/.ssh, or~/.aws/credentials. - Use the
--allowedToolsoption when running the agent to permit only necessary functions. This approach activates only the bare minimum tools, such as Bash, Read, or Write.
Using Claude Code's "Default" permission mode—which requires user approval for all write operations—can physically block the risk of source code leaks. A single configuration file is enough to prevent accidents where an agent sends environment variables externally without your permission.
How to Prevent a Looping Agent from Draining Your Wallet
Agent-centric tools plan and execute tasks autonomously. The problem arises when they fall into a logical fallacy. An agent that cannot find a solution may enter an infinite loop, repeating API calls and racking up dozens of dollars in an instant. According to research data from Anthropic, simply setting explicit "Exit Criteria" in the prompt can reduce execution time by up to 62%.
To prevent cost spikes, you must design "circuit breakers" directly into your prompts:
- Limit Attempt Counts: Add a constraint at the bottom of your command like: "If not resolved after a maximum of 5 attempts, stop immediately and report the cause of failure."
- Objective Success Criteria: Subjective instructions like "finish when the code looks good" are the worst. Instead, specify passing
npm testor a specific unit test as the absolute criterion for completion. - Context Limits: Using Glob patterns in your
.cursorrulesfile to restrict the agent to specific directory rules, such assrc/api/**/*, prevents unnecessary token waste.
Setting these exit gates suppresses token consumption caused by ambiguous instructions, potentially saving over 40% on average monthly API costs.
Isolated Workflows to Avoid Polluting the Main Branch
Allowing an agent to modify local files directly is fast but dangerous, as unverified code could break the main branch. The Shopify engineering team, while operating their internal agent tool "Sidekick," introduced a method to cross-verify results with a separate model. We should also isolate agent-specific workspaces.
The cleanest way is to use git worktree. Create an independent directory and branch dedicated to the agent session. Once the task is complete, have it run git diff to provide a summary report and automatically execute unit tests with tools like Playwright or Vitest. This process can reduce manual review time by 70% while ensuring only verified code is merged into the main branch.
Adjusting IDE Settings That Hinder Agent Speed
Agents write code much faster than humans can type. If VS Code's editor.formatOnSave feature is enabled, the file format might change forcibly while the agent is writing, causing text matching errors. It’s like the tool is tripping over its own feet.
Additionally, non-interactive shells executed by agents often fail to read environment variables set in .zshrc, leading to tool execution failures.
- Disable
editor.formatOnSaveby setting it tofalsein the collaborative project's.vscode/settings.json. - Move PATH or API key settings from
.zshrcto the.zshenvfile, which loads regardless of the shell execution method. - Instead, use
Huskyto configure pre-commit hooks so that linters and formatters run only when the agent commits after finishing its work.
If you don't want to see an agent standing around dumbfounded because it can't find environment variables, you must check your shell configuration first.
| Setting Category | File Name | Recommended Configuration | Agent Visibility |
|---|---|---|---|
| Global Environment Variables | .zshenv | PATH, API_KEY, SDK paths | Loaded in all sessions |
| Interactive Settings | .zshrc | alias, prompt, theme | May be ignored during agent execution |
| Login Settings | .zprofile | System-wide initialization scripts | Valid only in login shells |
