Build a Test-Fixing Coding Agent with Claude Code
Build One Useful Agent Before Adding Complexity
Building a coding agent with Claude Code does not require writing your own tool-execution loop. Claude Code already provides tools for inspecting files, editing code, and executing commands; its non-interactive mode makes those capabilities scriptable. This quick-start turns that foundation into a narrowly scoped test-fixing agent. Start locally, review every patch, and automate only after the workflow proves reliable. ([code.claude.com](https://code.claude.com/docs/en/headless))
1. Install Claude Code and Prepare Your Repository
On macOS with Homebrew, run the following installation command, then verify the executable. ([code.claude.com](https://code.claude.com/docs/en/quickstart))
brew install --cask claude-code
claude --version
For this walkthrough, use a trusted Node.js repository with dependencies installed and an npm test script that terminates rather than entering watch mode. First run the tests yourself to record existing failures. Check git status, save unrelated work, and create a branch:
git switch -c agent/fix-tests
2. Connect Claude Code to 59API
59API offers low-cost, pay-as-you-go access to Claude and GPT models, compatibility with Claude Code, Codex, and OpenAI SDK clients, and a referral rebate. It advertises native, official-quality models without downgrades. That combination makes it a practical option for experimenting with agent workflows without committing to a large subscription. Check current model prices before choosing. ([59api.com](https://59api.com/blog/your-first-claude-api-call-with-59api-in-5-minutes-3977?utm_source=openai))
Generate a 59API key and copy an available Claude model identifier from your dashboard. In a macOS or Linux shell, run these commands individually, replacing both placeholders. The authentication pattern below follows 59API’s published Claude Code configuration. ([59api.com](https://59api.com/?lang=en&utm_source=openai))
unset ANTHROPIC_API_KEY
export ANTHROPIC_BASE_URL="https://api.59api.com"
export ANTHROPIC_AUTH_TOKEN="YOUR_59API_KEY"
export CLAUDE_MODEL_ID="YOUR_SUPPORTED_CLAUDE_MODEL_ID"
Use a secret manager for recurring jobs rather than placing credentials in tracked files. Launch subsequent commands from this same terminal.
3. Give the Agent a Small Operating Contract
Create CLAUDE.md in the repository root. Claude Code loads project instructions from this file, making it useful for recording commands, conventions, and boundaries. Keep instructions concrete rather than adding pages of generic advice. ([code.claude.com](https://code.claude.com/docs/en/memory))
- Goal: Fix the reported failing test with the smallest reasonable patch.
- Validation: Run npm test; report the command and outcome.
- Boundaries: Do not weaken assertions, remove tests, change dependencies, or modify generated files.
- Handoff: Summarize changed files, remaining failures, and uncertainties.
Treat these as behavioral instructions, not security controls. Configure actual tool permissions separately. ([code.claude.com](https://code.claude.com/docs/en/memory))
4. Prove the Workflow Interactively
Start an interactive session with your selected model. Claude Code supports selecting a model through the --model flag. ([code.claude.com](https://code.claude.com/docs/en/cli-reference))
claude --model "$CLAUDE_MODEL_ID"
Give it a bounded task: “Investigate the failing date-parser test. Read the implementation and test first, explain the cause, then make the smallest fix. Run npm test. Do not commit.”
Approve only expected operations. Review the resulting diff yourself, paying particular attention to deleted assertions, unrelated edits, and changes that merely hide the failure.
5. Turn the Task into a Repeatable Agent Run
Once the interactive approach works, use print mode for a non-interactive run. This example exposes only three tools, pre-approves file reads and edits plus the specified test command, and limits the run to eight agent turns. Other shell commands are not pre-approved. ([code.claude.com](https://code.claude.com/docs/en/headless))
claude --model "$CLAUDE_MODEL_ID" -p 'Fix the failing date-parser test. Follow CLAUDE.md, run npm test, and summarize the patch. Do not commit.' --tools 'Read,Edit,Bash' --allowedTools 'Read,Edit,Bash(npm test)' --max-turns 8 --output-format json
JSON output gives your wrapper a machine-readable result and session metadata. Check process status and reported errors, then run tests independently: a successful agent invocation is not proof of a correct patch. ([code.claude.com](https://code.claude.com/docs/en/headless))
6. Keep Costs and Permissions Bounded
Start with one failure, not “clean up the repository.” Set an external job timeout, keep credentials out of logs, and require human review before merging. An approved test command can execute repository-defined scripts, so use a disposable environment without production secrets. Tool approvals are not a sandbox. ([code.claude.com](https://code.claude.com/docs/en/permissions))
Compare completed-task spending against your 59API balance rather than treating Claude Code’s client-side cost estimate as the relay’s invoice. ([code.claude.com](https://code.claude.com/docs/en/headless))
If this workflow fits your team, consider signing up for 59API with a small balance and measuring cost per reviewed, passing patch before scaling.
Ready to get started?
Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.
Sign up free