How to Use the Claude Code Harness via the SDK
🇧🇷 Leia em Português
Most people use Claude Code as a CLI. You open a terminal, talk to the agent, and it edits files, runs commands and reviews PRs.
You can drive that same harness from your own code: tools, system prompt, agent loop and skills. Nothing built from scratch.
The problem
If you want an agent that works on your codebase, the obvious path is to take the Anthropic API and wire everything by hand. You define the tools, write the system prompt, implement the tool-use loop, and handle context and permissions. That is a lot of code before you reach the part you care about.
And you end up with a worse version of what Claude Code already does.
The preset
@anthropic-ai/claude-agent-sdk has a preset that turns on all of Claude Code with one line per piece:
import { query, startup } from "@anthropic-ai/claude-agent-sdk";
await startup({ initializeTimeoutMs: 10_000 });
const result = query({
prompt: "review PR #42",
options: {
tools: { type: 'preset', preset: 'claude_code' },
systemPrompt: { type: 'preset', preset: 'claude_code' },
skills: ['pr-review'],
permissionMode: 'bypassPermissions',
},
});
for await (const message of result) {
// handle messages: assistant turns, tool results, etc.
}
The harness comes in two lines: { type: 'preset', preset: 'claude_code' } for tools and for systemPrompt. The rest is iterating over messages.
How it works underneath
The SDK does not call the Anthropic API directly. It spawns the claude CLI as a subprocess and talks to it over stdin/stdout in JSON. That has three consequences:
- You need the
claudeCLI installed. - The tools are the same ones you use in the CLI.
- Update the CLI and your agent’s behavior changes with it.
What you get for free
- Built-in tools: Read, Write, Edit, Bash, Grep, Glob, Agent, TodoWrite, WebSearch and more.
- Skills, loaded from
~/.claude/skills/withskills: ['skill-name']. - Session management: resuming conversations and keeping context.
- Context compaction: the SDK manages context on its own.
- Lifecycle hooks to intercept and change behavior.
- Support for MCP (Model Context Protocol) servers.
Reference
ChatML is a real project doing exactly this. It is a macOS desktop app that uses the same SDK with the claude_code preset to offer the Claude Code experience in a native interface.
On our team we use an internal wrapper called cc-harness, which is not public. It is ~300 lines on top of the SDK that do the first pass of code review on a project’s PRs. It loads the review skills, opens the app in Chrome through MCP to test frontend changes, and comments on the PR.
It never approves anything. The AI does the first pass, and a person does the second.
If you already use Claude Code and want to automate it or embed it in a product, the SDK is the shortest path. There is no need to reimplement what already exists.
Thanks for reading!