Claude Agent SDK setup guide
Build AI agents that can read files, run commands, and edit code using the Claude Agent SDK and Trigger.dev.
The Claude Agent SDK gives you the same tools, agent loop, and context management that power Claude Code. Combined with Trigger.dev, you get durable execution, automatic retries, and full observability for your agents.
Setup
This guide assumes you are working with an existing Trigger.dev project. Follow our quickstart to get set up if you don't have a project yet.
npm install @anthropic-ai/claude-agent-sdkAdd the SDK to the external array so it's not bundled:
import { } from "@trigger.dev/sdk";
export default ({
: process..TRIGGER_PROJECT_REF!,
build: {
: ["@anthropic-ai/claude-agent-sdk"],
},
: "small-2x",
});Adding packages to external prevents them from being bundled, which is necessary for the Claude
Agent SDK. See the build configuration docs for more details.
Add your Anthropic API key to your environment variables. The SDK reads it automatically.
ANTHROPIC_API_KEY=sk-ant-...You can set this in the Trigger.dev dashboard under Environment Variables, or in your .env file for local development.
This example creates a task where Claude generates code in an empty workspace. The agent will create files based on your prompt:
import { } from "@anthropic-ai/claude-agent-sdk";
import { , } from "@trigger.dev/sdk";
import { , , } from "node:fs/promises";
import { } from "node:os";
import { } from "node:path";
import { z } from "zod";
export const codeGenerator = ({
: "code-generator",
schema: z.object({
prompt: z.string(),
}),
: async ({ prompt }, { }) => {
const abortController = new ();
.("abort", () => abortController.());
// Create an empty workspace for the agent
// The agent will create files here based on the prompt
const workDir = await (((), "claude-agent-"));
.info("Created workspace", { workDir });
try {
const result = ({
prompt,
options: {
model: "claude-sonnet-5-5",
abortController,
cwd: workDir,
maxTurns: 10,
permissionMode: "acceptEdits",
allowedTools: ["Read", "Edit", "Write", "Glob"],
},
});
for await (const message of result) {
.info("Agent message", { type: message.type });
}
// See what files Claude created
const files = await (workDir, { recursive: true });
.info("Files created", { files });
return { filesCreated: files };
} finally {
await (workDir, { : true, : true });
}
},
});npx trigger.dev@latest devpnpm dlx trigger.dev@latest devyarn dlx trigger.dev@latest devbunx trigger.dev@latest devGo to the Trigger.dev dashboard, find your code-generator task, and trigger it with a test payload:
{
"prompt": "Create a Node.js project with a fibonacci.ts file containing a function to calculate fibonacci numbers, and a fibonacci.test.ts file with tests."
}How it works
The query() function runs Claude in an agentic loop where it can:
- Read files - Explore codebases with
Read,Grep, andGlobtools - Edit files - Modify code with
EditandWritetools - Run commands - Execute shell commands with
Bashtool (if enabled) - Think step by step - Use extended thinking for complex problems
The agent continues until it completes the task or reaches maxTurns.
Permission modes
| Mode | What it does |
|---|---|
"default" | Asks for approval on potentially dangerous operations |
"acceptEdits" | Auto-approves file operations, asks for bash/network |
"bypassPermissions" | Skips all safety checks (not recommended) |
Available tools
allowedTools: [
"Task", // Planning and task management
"Glob", // Find files by pattern
"Grep", // Search file contents
"Read", // Read file contents
"Edit", // Edit existing files
"Write", // Create new files
"Bash", // Run shell commands
"TodoRead", // Read todo list
"TodoWrite", // Update todo list
];GitHub repo
View the Claude Agent SDK + Trigger.dev example
A complete example with two agent patterns: basic safe code generation and advanced with bash execution.
Deploy this example to your project
Copies claude-agent-sdk-trigger from GitHub, installs it, and deploys its tasks to your project’s production environment.
npx degit triggerdotdev/examples/claude-agent-sdk-trigger claude-agent-sdk-trigger
cd claude-agent-sdk-trigger
npm install
npx trigger.dev@latest login
npx trigger.dev@latest deploy --project-ref <your-project-ref>If the example has a .env.example, set those variables in your project’s environment variables before deploying. To run the tasks on your machine instead, use dev in place of deploy.
Example projects using the Claude Agent SDK
Claude changelog generator
Generate changelogs from git commits using custom MCP tools.
Claude GitHub wiki agent
Analyze repositories and answer questions with real-time streaming.
Learn more
- Claude Agent SDK docs – Official Anthropic documentation
- Trigger.dev Realtime – Stream agent progress to your frontend
- Waitpoints – Add human-in-the-loop approval steps
Was this page helpful?
Last updated
Edit on GitHub