docs/guides/ai-agents/claude-code-trigger.mdx
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.
npm install @anthropic-ai/claude-agent-sdk
Add the SDK to the external array so it's not bundled:
import { defineConfig } from "@trigger.dev/sdk";
export default defineConfig({
project: process.env.TRIGGER_PROJECT_REF!,
build: {
external: ["@anthropic-ai/claude-agent-sdk"],
},
machine: "small-2x",
});
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 { query } from "@anthropic-ai/claude-agent-sdk";
import { schemaTask, logger } from "@trigger.dev/sdk";
import { mkdtemp, rm, readdir } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { z } from "zod";
export const codeGenerator = schemaTask({
id: "code-generator",
schema: z.object({
prompt: z.string(),
}),
run: async ({ prompt }, { signal }) => {
const abortController = new AbortController();
signal.addEventListener("abort", () => abortController.abort());
// Create an empty workspace for the agent
// The agent will create files here based on the prompt
const workDir = await mkdtemp(join(tmpdir(), "claude-agent-"));
logger.info("Created workspace", { workDir });
try {
const result = query({
prompt,
options: {
model: "claude-sonnet-4-20250514",
abortController,
cwd: workDir,
maxTurns: 10,
permissionMode: "acceptEdits",
allowedTools: ["Read", "Edit", "Write", "Glob"],
},
});
for await (const message of result) {
logger.info("Agent message", { type: message.type });
}
// See what files Claude created
const files = await readdir(workDir, { recursive: true });
logger.info("Files created", { files });
return { filesCreated: files };
} finally {
await rm(workDir, { recursive: true, force: true });
}
},
});
npx trigger.dev@latest dev
Go 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."
}
The query() function runs Claude in an agentic loop where it can:
Read, Grep, and Glob toolsEdit and Write toolsBash tool (if enabled)The agent continues until it completes the task or reaches maxTurns.
| 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) |
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
];
<Card title="View the Claude Agent SDK + Trigger.dev example" icon="GitHub" href="https://github.com/triggerdotdev/examples/tree/main/claude-agent-sdk-trigger"
A complete example with two agent patterns: basic safe code generation and advanced with bash execution. </Card>