Build custom agents with Claude Agent SDK
✓Works with OpenClaudeYou are a backend engineer setting up the Claude Agent SDK to build custom autonomous agents. The user wants to initialize a project, configure the SDK, and create a working agent that can interact with tools.
What to check first
- Verify Node.js version 18+ is installed:
node --version - Check your Anthropic API key is set:
echo $ANTHROPIC_API_KEY(should output your key, not be empty) - Confirm you have npm or yarn available:
npm --version
Steps
- Create a new project directory and initialize npm:
mkdir my-agent && cd my-agent && npm init -y - Install the Anthropic SDK:
npm install @anthropic-ai/sdk - Create an
agent.jsfile and import the Anthropic client withrequire("@anthropic-ai/sdk") - Initialize the Anthropic client using your API key from environment variables
- Define tool schemas using the
toolsparameter — each tool needsname,description, andinput_schemawith JSON Schema format - Create the agentic loop using
messages.create()withmodel: "claude-3-5-sonnet-20241022"andmax_tokens: 4096 - Check the response
stop_reason— if it's"tool_use", process tool calls fromcontentarray - Execute the actual tool function based on the tool name, then add results back to messages with
role: "user"andcontentcontaining atool_resultblock - Continue looping until
stop_reasonis"end_turn"
Code
const Anthropic = require("@anthropic-ai/sdk");
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
// Define tools the agent can use
const tools = [
{
name: "get_weather",
description: "Get the current weather for a location",
input_schema: {
type: "object",
properties: {
location: {
type: "string",
description: "City name",
},
},
required: ["location"],
},
},
{
name: "calculate",
description: "Perform mathematical calculations",
input_schema: {
type: "object",
properties: {
expression: {
type: "string",
description: "Math expression to evaluate",
},
},
required: ["expression"],
},
},
];
// Simulated tool implementations
function executeToolCall(toolName, toolInput) {
if (toolName === "get_weather") {
return `Weather in ${toolInput.location}: 72°F, Sunny`;
} else if (toolName === "calculate") {
return `Result: ${eval(toolInput.expression)}`;
}
return "Unknown tool";
}
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Claude Code Skills
Other Claude Code skills in the same category — free to download.
CLAUDE.md Writer
Write effective CLAUDE.md project configuration files for Claude Code
MCP Server Setup
Set up Model Context Protocol servers for Claude Code
Custom Slash Commands
Create custom slash commands for Claude Code workflows
Hooks Configuration
Configure Claude Code hooks for automated pre/post actions
Skills Writer
Write custom Claude Code skill files with proper format
Memory Setup
Configure Claude Code persistent memory system
Permissions Config
Configure Claude Code permission settings and tool access
Context Management
Optimize context window usage in Claude Code conversations
Want a Claude Code skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.