Skip to content

claude-sdk-agent.ts

Source file: 02-agents/templates/claude-sdk-agent.ts. Copy it from the repository, or use the copy button on the code block.

claude-sdk-agent.ts
/**
* Claude Agent SDK template: summarise a PR diff into summary.md.
*
* Setup: npm install @anthropic-ai/claude-agent-sdk
* Auth: ANTHROPIC_API_KEY (or Bedrock / Vertex environment variables)
* Run: npx tsx claude-sdk-agent.ts
*
* Exit codes: 0 = success, 1 = agent error or max turns reached.
* Check option names against the current SDK docs; the surface evolves.
*/
import { query } from "@anthropic-ai/claude-agent-sdk";
const PROMPT = `
Summarise the changes in \`git diff origin/main...HEAD\`.
Write the result to summary.md using exactly these headings:
## What changed
## Why it matters
## Risks
Keep it under 200 words. Do not modify any other file.
`;
async function main(): Promise<number> {
let exitCode = 1;
for await (const message of query({
prompt: PROMPT,
options: {
systemPrompt: "You are a release engineer. Be concise and factual.",
allowedTools: ["Read", "Grep", "Glob", "Bash", "Write"],
permissionMode: "acceptEdits",
maxTurns: 20,
// settingSources: ["project"], // load .claude/ skills, agents, CLAUDE.md
// mcpServers: { github: { command: "npx", args: ["-y", "@modelcontextprotocol/server-github"] } },
// agents: { "diff-reader": { description: "...", prompt: "...", tools: ["Read", "Bash"] } },
},
})) {
if (message.type === "result") {
console.error(`result=${message.subtype} turns=${message.num_turns} cost=$${message.total_cost_usd}`);
exitCode = message.subtype === "success" ? 0 : 1;
}
}
return exitCode;
}
process.exit(await main());