A note on evidence: both snippets below were type-checked on November 25, 2026 against @anthropic-ai/claude-agent-sdk 0.3.332 with TypeScript 7.0.2 under strict, and the permission rules are taken from Anthropic's SDK documentation as it read that day. No agent run is reported here, so there's no output to show.
Put Bash in allowedTools, point the agent at a repo with a half-finished migration script in it, and it can run npm run migrate without asking anyone, because pre-approving a tool means exactly that. Picture that on a branch you care about and you've understood the main thing about this SDK: the agent it gives you acts on the machine it runs on.
Claude Agent SDK vs the Claude API
The plain Claude API gives you one thing: a request goes in, a response comes back. Multi-step reasoning, tool calls, and file access are all things you build yourself on top of it. The Claude Agent SDK sits a layer above that: it's the same agent loop that powers Claude Code, packaged as an npm library you call from your own Node process.
Install it with npm install @anthropic-ai/claude-agent-sdk. That single package gives you query(), the built-in tool set (Read, Write, Edit, Bash, Glob, Grep, WebSearch, WebFetch), permission hooks, and session resume, all without shelling out to a CLI binary. If you've used Claude Code from the terminal, this is that same engine, embedded in your own TypeScript code instead of a chat interface.
Getting an Anthropic API key
You need a key before any of the code below will run. The quickstart is brief about it: create one in the Claude Console, then export it in the shell that runs your agent.
export ANTHROPIC_API_KEY=your-api-key
Two details from the same page save a confused first run. The SDK reads the key from the process environment and "doesn't load .env files automatically", so if you keep it in .env, load it yourself with something like dotenv. And Node.js 18.14 or newer is the stated minimum. Billing is per token, the same as any other API usage, which is why a spend limit on the workspace is worth setting before an agent loop starts retrying a failing tool call.
Can you skip the key and reuse a Claude subscription login? Not for something you ship: the docs say third-party developers may not offer claude.ai login for products built on the SDK. Bedrock, Google Cloud and Foundry credentials are the documented alternatives.
Install it and run a first agent
Set your API key, install the package, and call query(). It returns an async generator, so you stream messages with a plain for await loop:
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "List the TypeScript files in this project and summarize what each one does.",
options: {
cwd: process.cwd(),
tools: ["Read", "Glob"],
allowedTools: ["Read", "Glob"],
permissionMode: "dontAsk",
},
})) {
if (message.type === "assistant") {
console.log(message.message);
}
if (message.type === "result") {
console.log("done:", message);
}
}
Four options matter more than the rest on your first run, and they work on two different layers.
tools is availability. With tools: ["Read", "Glob"], only those built-ins are in Claude's context; Bash, Write and Edit aren't there to call. This is the option the permissions docs and quickstart point to when you want to limit the built-in set.
allowedTools is permission. It pre-approves the tools it lists, so Read and Glob run without a prompt. On its own it isn't a fence: the docs put it bluntly, any tool not listed "is still available to Claude", and a call to it that needs approval falls through to the permission mode and then to a canUseTool callback, if you passed one.
permissionMode: "dontAsk" turns every would-be prompt into a denial. Is that a complete lock? No. Calls that need no approval in the first place still run, and the docs name read-only Bash commands, the Agent tool and file reads inside your working directories. That's why this example sets tools as well.
cwd sets the working directory. It isn't a sandbox. Reads inside it need no approval; a read outside it would prompt, which dontAsk denies.
A custom tool with tool() and Zod
Built-in tools cover file access and shell commands. For anything domain-specific, an internal API, a database query, a linter you wrote, you define your own with tool() and a Zod schema:
import { tool, createSdkMcpServer, query } from "@anthropic-ai/claude-agent-sdk";
import { z } from "zod";
const lintCheck = tool(
"lint_check",
"Run the project's ESLint config against a file path and return violations",
{ filePath: z.string() },
async ({ filePath }) => {
const violations = await runEslint(filePath); // your own function
return {
content: [{ type: "text", text: JSON.stringify(violations) }],
};
},
);
const devTools = createSdkMcpServer({
name: "dev-tools",
tools: [lintCheck],
});
for await (const message of query({
prompt: "Check src/index.ts for lint violations and explain the worst one.",
options: {
mcpServers: { "dev-tools": devTools },
allowedTools: ["mcp__dev-tools__lint_check"],
},
})) {
console.log(message);
}
The Zod schema does double duty here. It validates the arguments Claude sends before your handler ever runs, and TypeScript infers the handler's argument types straight from it, so filePath is a string with zero manual typing. The description string is the only thing the model sees when deciding whether to call this tool, so write it like documentation, not a variable name. Note the naming convention on the allowlist too: in-process tools get addressed as mcp__<server>__<tool>, and getting that string wrong is an easy way to end up with a custom tool that silently never fires. The agent just won't have it available, and nothing in the output says why. A server name written with a hyphen in mcpServers and an underscore in allowedTools is enough to do it.
Permission modes for production
Six modes exist in SDK 0.3.332: default, dontAsk, acceptEdits, plan, auto and bypassPermissions. The table gives what the docs say each one does with a call that needs approval.
| Mode | What happens |
|---|---|
default | no automatic approvals; a call that matches no allow rule goes to your canUseTool callback, and is denied if nothing approves it |
dontAsk | the call is denied without calling canUseTool; pre-approved tools still run |
acceptEdits | file edits and filesystem commands such as mkdir, mv and rm inside the working directories are approved; other Bash still needs permission |
plan | Claude explores and plans; edits made with its file tools go to canUseTool even when an allow rule matches |
auto | a classifier model reviews actions such as shell commands and network requests |
bypassPermissions | tools run without prompts, apart from a short list of actions no mode auto-approves |
Most of the safety decisions sit between dontAsk and bypassPermissions. The docs carry a warning in bold: allowedTools does not constrain bypassPermissions. Set allowedTools: ["Read"] with that mode and calls to Bash, Write and Edit are still approved. Only deny rules, ask rules and hooks are checked before the mode.
For a CI script or a scheduled job, don't reach for bypassPermissions just because there's no human around to click "approve." The docs' own recipe for a locked-down agent is allowedTools plus permissionMode: "dontAsk", and I'd add tools (or disallowedTools: ["Bash"]) so the tools you don't want aren't offered at all. What about leaving it on default? With Claude Code 2.1.302 or later behind the SDK, a call that needs approval and has no allow rule, hook or callback to approve it is denied, so nothing hangs, but you're relying on a default instead of stating the rule. Reserve bypassPermissions for a throwaway container, not a real repo.
canUseTool is the callback for calls nothing else resolved. It's an async function you pass in options that receives the tool name and its input and returns allow or deny. One trap is documented and easy to walk into: a tool approved earlier never reaches it. List bare Bash in allowedTools and your callback won't see a single Bash call; the TypeScript SDK even emits a process warning with the code CLAUDE_SDK_CAN_USE_TOOL_SHADOWED when it spots that combination. If you want to inspect commands, either leave Bash out of allowedTools, pre-approve only a scoped rule such as Bash(npm test *), or put the check in a PreToolUse hook, which runs for approved calls too.
Where the SDK fits next to MCP and agent frameworks
If you've already built MCP servers for Claude Code or Cursor, nothing here replaces them, mcpServers in the query() options accepts the same external MCP servers over stdio or HTTP, alongside your in-process tool() definitions. The mechanics of that protocol are covered at the end of this section. And if you're comparing this against a framework-first approach instead of Anthropic's own SDK, Mastra and the Vercel AI SDK covers that other path, provider-agnostic, more scaffolding, less tied to Claude specifically. Moving one of these tools out into a standalone server later? The typed-schema side of that, and which spec version your client actually speaks, is in MCP spec versions and typed tools.
The tradeoff: the Claude Agent SDK gets you Claude Code's actual agent loop with almost no glue code, at the cost of being Anthropic-specific. If your product is committed to Claude anyway, that's not a real cost. If you need to swap models later, budget the time to rebuild this layer on something provider-agnostic. For a sense of scale, the two code examples in this guide come to under 50 lines of TypeScript together.