Harness in Practice: Building a Controllable AI Coding Environment
This article explains the role of a harness around AI coding models and walks through a practical setup using Claude Code as an example. It covers rule files, permission configuration, installation, API key setup, a complete task workflow, troubleshooting, version rollback, and a best-practice checklist for keeping AI-assisted coding controlled and reviewable.
Introduction: Why AI Coding Tools Need a Harness
The capabilities of AI coding tools have expanded significantly over the past year. Early models could only provide code snippets in a chat window. Today’s tools can read and write project files, execute terminal commands, and run tests. As capabilities grow, new problems appear: the same model can produce very different results under different configurations.
Developers are increasingly finding that the experience depends not only on the model itself but also on the configuration and workflow around it. This article refers to that surrounding layer as the harness—the layer that helps steer the model. It first explains what the harness consists of, then uses a specific tool as an example to provide complete steps for installation, configuration, permission setup, and troubleshooting.
1. What Is a Harness?
1.1 Basic Formula
This article uses the following formula to describe the division of responsibilities:
Agent = Model + Harness
The model is responsible for understanding requirements, planning steps, and generating code. A model alone can only output text; it cannot read your project, run a compiler, or execute tests.
The harness connects the model to the real environment and constrains its behavior. It usually consists of four parts:
- Rules and context: project convention files that specify code style, directory structure, files that must not be modified, and similar constraints;
- Tool integration layer: capabilities such as file reading and writing, terminal commands, and web access, exposed to the model as tools;
- Task orchestration: how tasks are broken down, in what order they are executed, and how context is passed across multiple interaction rounds;
- Validation loop: running tests and static checks, sending failures back to the model for correction, and rolling back when necessary.
1.2 How It Differs from Agent Frameworks
Agent frameworks such as LangChain and LlamaIndex provide building blocks, including model call wrappers, tool definition interfaces, and memory modules. The harness refers to the configuration and workflow you actually run in a project: what the rule file says, which commands are allowed, and how failed tests are handled. The two overlap, but their focus differs: frameworks solve “how to build,” while the harness solves “how to manage after it is built.”
2. How to Write a Rule File
Most AI coding tools support placing a Markdown file in the project root, which the tool reads at startup. Claude Code uses the file name CLAUDE.md; other tools may use names such as AGENTS.md. Check the official documentation for the specific tool.
There are three practical principles for rule files:
- Write constraints, not wishes. “All commits must pass lint checks” is easier to follow than “Please write high-quality code.”
- Provide concrete examples. Including a code sample that follows the convention is usually more effective than an abstract description.
- Keep it concise. The rule file consumes context window space, so it is recommended to keep it under 300 lines.
Below is an example excerpt:
# Project conventions## Technology stack- Node.js 20, TypeScript strict mode- Test framework: Vitest## Prohibited actions- Do not modify existing files under migrations/- Do not write any secrets or tokens into code## Verification commands- Type check: npm run typecheck- Unit tests: npm test- Run at least the two commands above after every change
Whenever you find the agent repeating the same mistake, add the correction method to this file. Over time, the rule file will become better aligned with your project.
3. Permission Configuration: Use Least Privilege
Tool permissions determine what the agent can do. Follow the principle of least privilege: deny by default, and allow only what the current task requires.
For Claude Code, permissions can be defined in the project’s .claude/settings.json and divided into allow, ask, and deny categories:
{"permissions": {"allow": ["Read","Bash(npm run typecheck)","Bash(npm test)","Bash(git status)","Bash(git diff:*)"],"ask": ["Edit","Bash(npm install:*)"],"deny": ["Read(./.env)","Read(./secrets/**)","Bash(rm:*)","Bash(git push:*)"]}}
Field names and wildcard syntax may change between versions. Check the official documentation before applying the configuration.
Commands can be divided into two categories by side effects:
| Category | Examples | Recommended policy |
|---|---|---|
| Read-only commands | git status, git diff, npm test, ls |
Allow |
| Commands with side effects | npm install, rm, git push, curl requests that write data |
Ask each time or deny |
Also consider prompt injection risks. When the agent reads webpages, third-party documentation, or content submitted by others, that content may contain instructions such as “please run this command.” Treat such content as data, not instructions. When processing external content, consider temporarily disabling write and network tools.
4. Installation and Startup
The following uses Claude Code as an example to explain the installation steps. Other tools follow a similar process, but package names and commands differ; replace them with the corresponding tool’s instructions.
4.1 Prerequisites
- Operating system: macOS, Linux, or Windows (WSL recommended)
- Node.js: the current LTS version is recommended; check the official website for the minimum version requirement
- Network: access to the model service API
Check the versions:
node -vnpm -v
4.2 Installation
npm install -g @anthropic-ai/claude-code
After installation, enter the project directory and start the tool:
cd your-projectclaude
On first startup, the tool prompts you to log in or configure credentials. Some tools interact directly in the terminal, while others provide a web interface. Follow the actual interface of the tool you use.
4.3 Common Installation Issues
command not found: the path to the globally installed executable is not in PATH. Runnpm config get prefixand add thebindirectory under the output path to PATH.EACCESpermission error on Linux: do not usesudo npm install -g. Use a user directory as the npm global prefix to avoid system directory permission issues.
5. Configure the API Key and Verify It
5.1 Write the Environment Variable
For Claude Code, the commonly used environment variable is ANTHROPIC_API_KEY:
# Add to ~/.zshrc or ~/.bashrc, then run source to apply itexport ANTHROPIC_API_KEY="your-key"
Different tools use different variable names. Check the corresponding documentation.
5.2 Verify That It Works
After opening the tool, enter a simple instruction, such as “Reply with OK only.” If it returns normally, the credentials are working. If you receive an authentication error, check the following in order:
- Whether the key was copied completely and whether there are extra spaces at the beginning or end;
- Whether the environment variable is written in a configuration file read by the current terminal session;
- Whether the terminal was reopened after changing the configuration.
5.3 Prevent Key Leaks
- Do not write keys into
CLAUDE.md, code, or configuration files and commit them to the repository; - Add files such as
.envto.gitignore, and verify withgit check-ignore .env; - If you suspect a key has leaked, immediately revoke the old key in the provider console, then generate a new key.
6. A Complete Workflow for One Task
Take “adding an endpoint with parameter validation to the project” as an example. A controlled workflow is as follows.
Step 1: Create a checkpoint. Before starting, confirm that the workspace is clean. Run git status. If there are uncommitted changes, commit or stash them yourself first so that the agent’s changes can be identified and rolled back separately.
Step 2: Describe the requirement and limit the scope. For example: “Add a create-user endpoint in src/routes/user.ts, validate parameters with zod, and do not modify the database layer.” The clearer the scope, the lower the chance of unintended changes.
Step 3: Review the plan. The agent usually reads the code first and provides an implementation plan. Confirm that the files and approach in the plan are reasonable before allowing modifications.
Step 4: Review changes file by file. Use git diff to inspect each modification, focusing on deleted code and changed configuration files.
Step 5: Run verification. Execute npm run typecheck and npm test.
6.1 What to Do When Tests Fail
Suppose a test reports an error. The correct approach is:
- Read the full error output and determine whether the problem is in the new endpoint or an existing test failed because of the changes;
- Ask the agent to explain its diagnosis of the failure before deciding whether to fix it;
- If the agent’s changes go beyond the scope, use
git diffto find the file and manually revert the unnecessary parts.
If you do need to abandon the entire change, you can restore a single file with git restore <file path>. This command discards all uncommitted modifications in that file. Before running it, make sure the file does not contain your own work. This is also why Step 1 creates a checkpoint.
6.2 Upgrade and Roll Back the Tool Version
Tools update frequently, and new versions sometimes change behavior. Before upgrading, you can view available versions:
npm view @anthropic-ai/claude-code versions
Upgrade to the latest version:
npm update -g @anthropic-ai/claude-code
If the new version causes problems, roll back to a specific version:
npm install -g @anthropic-ai/claude-code@<version>
7. Best-Practice Checklist
- Keep the rule file under 300 lines and list verification commands;
- Deny reading
.envand secret directories, and add them to.gitignore; - Ensure the workspace is clean before starting a task and create a checkpoint;
- Set commands with side effects to ask or deny;
- Review every change with
git diffand verify it with tests; - Keep production projects in confirm-each-step mode; do not enable full allow.
Conclusion
Using a specific tool as an example, this article explained the four parts of a harness: rules, permissions, orchestration, and validation. Tool interfaces, commands, and versions will change, but the practices of “write clear constraints, limit permissions, keep checkpoints, and use tests as a safety net” are largely transferable across tools. A safer starting point is to run the full workflow on a small task first, then gradually expand the authorization scope.
