The Agent SDK: Building Your Own Claude
Claude Code is Claude Code. But what if you wanted to build something different? Something specialized?
The Agent SDK lets you create custom agents powered by Claude's intelligence but shaped by your architecture.
What The Agent SDK Provides
import { Agent, Tool, Memory } from '@anthropic-ai/agent-sdk';
const myAgent = new Agent({
model: 'claude-opus-4-5-20251101',
tools: [readFile, writeFile, runTests],
memory: persistentMemory,
instructions: 'You are a specialized testing agent...'
});
You're not wrapping an API. You're constructing an autonomous system.
Core Components
1. Tools
Tools are functions your agent can call. Claude Code has built-in tools. Your agent can have custom ones.
const deployTool = Tool.create({
name: 'deploy',
description: 'Deploy the application to specified environment',
parameters: {
environment: { type: 'string', enum: ['staging', 'production'] }
},
execute: async (params) => {
// Your deployment logic
}
});
2. Memory
Agents can persist information across sessions. Not just conversation history—structured memory.
const memory = Memory.create({
type: 'persistent',
storage: 'local', // or 'remote' for cloud persistence
schema: {
projectContext: 'string',
userPreferences: 'object',
learnings: 'array'
}
});
3. Instructions
System prompts on steroids. Not just personality—operating procedures.
instructions: `
You are a security-focused code reviewer.
ALWAYS:
- Check for SQL injection vulnerabilities
- Verify input sanitization
- Flag hardcoded credentials
NEVER:
- Suggest changes without explaining the security implication
- Approve code that hasn't passed SAST scanning
`
A Real Agent Example
Let's build something useful: a PR review agent.
import { Agent, Tool, Memory } from '@anthropic-ai/agent-sdk';
const prReviewAgent = new Agent({
name: 'PR Reviewer',
model: 'claude-opus-4-5-20251101',
tools: [
Tool.builtin('read_file'),
Tool.builtin('git_diff'),
Tool.create({
name: 'add_review_comment',
description: 'Add a review comment to the PR',
parameters: {
file: 'string',
line: 'number',
comment: 'string',
severity: { enum: ['info', 'warning', 'critical'] }
},
execute: async (params) => {
return await githubApi.addComment(params);
}
})
],
memory: Memory.create({
type: 'persistent',
schema: {
reviewPatterns: 'array', // Patterns we've flagged before
projectStyle: 'object' // Learned code style
}
}),
instructions: `
Review PRs with focus on:
1. Logic errors and edge cases
2. Security vulnerabilities
3. Performance implications
4. Code style consistency with project patterns
Be specific. Reference line numbers.
Suggest fixes, don't just identify problems.
`
});
// Use it
const result = await prReviewAgent.run({
task: 'Review the changes in this PR',
context: { prNumber: 423, repo: 'acme/backend' }
});
This agent:
- Reads the PR diff
- Applies security and logic checks
- Remembers patterns from previous reviews
- Posts specific, actionable comments
Run it on every PR. It becomes part of your review process.
Subagents: Divide and Conquer
Here's where it gets interesting.
Claude Code can spawn subagents—smaller, focused instances that handle specific tasks while the main context continues.
The Pattern
You: "Refactor the authentication system"
Claude Code (main):
"This is a complex task. I'll break it down:
- Subagent 1: Analyze current auth patterns
- Subagent 2: Research best practices
- Subagent 3: Implement changes
Spawning agents..."
The main session orchestrates. Subagents execute. Results flow back.
Spawning Subagents
From within Claude Code, you can use the Task tool:
Break this into parallel tasks:
1. Find all deprecated API calls
2. Check test coverage gaps
3. Identify security vulnerabilities
Run these simultaneously and summarize findings.
Claude Code automatically:
- Creates three subagent contexts
- Assigns tasks to each
- Runs them in parallel
- Aggregates results
You get three agents' worth of work in one agent's time.
The Hierarchy
Main Agent (your Claude Code session)
├── Subagent: Code Explorer
│ └── Focused on reading and understanding
├── Subagent: Implementation
│ └── Focused on writing and modifying
└── Subagent: Verification
└── Focused on testing and validation
Each subagent is specialized. None is overwhelmed. The main agent coordinates.
This mirrors how senior engineers work. They don't do everything themselves. They delegate, coordinate, and synthesize.
Background Tasks: Async Everything
Sometimes you need work done, but you don't need to wait for it.
Running in Background
Run the full test suite in background.
Continue working on the API endpoint.
Alert me when tests finish.
The tests run. You keep working. Results appear when ready.
The Practical Application
Scenario: You're implementing a feature. It requires:
- Code changes (15 minutes of focus)
- Documentation updates (low priority)
- Test generation (can run async)
- Security scan (runs in background)
Old way: Do each sequentially. 2 hours.
New way:
Start these in background:
- Generate tests for UserService
- Update API documentation
- Run security scan on new endpoints
I'll focus on the implementation.
Background tasks execute while you work. Results accumulate. You review when ready.
Total time: 30 minutes of focused work plus review.
Hooks: Event-Driven Automation
Hooks let you trigger actions based on events in your Claude Code session.
Hook Types
| Hook |
Triggers When |
PreToolUse |
Before any tool runs |
PostToolUse |
After any tool completes |
Stop |
When Claude finishes responding |
SessionStart |
When a session begins |
Practical Hooks
Auto-Format Hook:
{
"hooks": {
"PostToolUse": [
{
"tool": "Write",
"command": "npx prettier --write $FILE"
}
]
}
}
Every time Claude writes a file, Prettier formats it. Automatically.
Security Check Hook:
{
"hooks": {
"PreToolUse": [
{
"tool": "Bash",
"command": "validate-command.sh $COMMAND",
"blocking": true
}
]
}
}
Before any bash command runs, your script validates it. Blocks dangerous commands.
Git Commit Hook:
{
"hooks": {
"Stop": [
{
"condition": "files_modified",
"command": "git add -p && git commit"
}
]
}
}
When Claude finishes making changes, prompt for a commit. No forgetting.
MCP: Model Context Protocol
This is the hidden giant.
MCP lets Claude Code talk to external systems. Not through you. Directly.
What MCP Enables
Before MCP:
You: "Check the Jira tickets for bug reports"
Claude: "I can't access Jira. Please paste the tickets."
You: [Goes to Jira, copies, pastes]
With MCP:
You: "Check the Jira tickets for bug reports"
Claude: [Queries Jira directly]
"Found 3 critical bugs. Here's the analysis..."
Setting Up MCP Servers
In your Claude configuration:
{
"mcpServers": {
"jira": {
"command": "npx",
"args": ["-y", "jira-mcp-server"],
"env": {
"JIRA_URL": "https://yourcompany.atlassian.net",
"JIRA_TOKEN": "${JIRA_TOKEN}"
}
},
"github": {
"command": "npx",
"args": ["-y", "github-mcp-server"],
"env": {
"GITHUB_TOKEN": "${GITHUB_TOKEN}"
}
}
}
}
Now Claude Code can:
- Query Jira tickets
- Read GitHub PRs and issues
- Create branches
- Post comments
- Close tickets
All within your natural conversation.
The Integration Pattern
Morning Workflow:
You: "What's my day look like?"
Claude: [Queries Jira for assigned tickets]
[Checks GitHub for PR reviews needed]
[Reads calendar via MCP]
"You have 3 tickets assigned, 2 PRs to review,
and a meeting at 2pm. Here's my suggested
priority order..."
One question. Full context. No tab switching.
The Jazz Ensemble
Here's where it comes together.
Individual features are instruments. The magic is the composition.
A Real Workflow:
- Start remote session – Share with your team
- Load project context – CLAUDE.md already configured
- Spawn subagents – One explores, one implements, one tests
- Hook into tools – Auto-format, auto-commit, security checks
- Connect via MCP – Jira, GitHub, Slack all accessible
- Background tasks – Tests and scans run continuously
You're not using a coding assistant. You're conducting an orchestra.
Each musician knows their part. You provide the direction. The music emerges.
The Intermediate Practitioner's Checklist
Before moving to Part 3 (the elite techniques), master these:
- Remote sessions for persistence and collaboration
- Agent SDK for custom automation
- Subagents for parallel execution
- Background tasks for async work
- Hooks for event-driven automation
- MCP for external integrations
These aren't advanced features. These are the actual features. Everything in Part 1 was just learning to read music.
Now you can play.
What Comes in Part 3
Part 3 is different. It's not about features. It's about emergence.
What happens when you push Claude Code beyond its documented capabilities?
What patterns have the top 0.1% discovered?
What can you do that Anthropic hasn't officially blessed?
That's next. And it's going to break your mental model one more time.
Part 3: Claude Code Elite—The Techniques Only 0.1% Know → Coming next
Comments · 0
No comments yet. Be the first to share your thoughts.