✻Founding-member pricing: every lesson for $19/mo. The first 3 lessons are free. Start lesson 1 →
Unit 8 · Automate Repeat Work · Lesson 36 of 40

Build Agents with the Claude Agent SDK

Use the Claude Agent SDK in Python or TypeScript to build your own agents on Claude Code's engine: query(), options, tools, permissions and streaming.

ACADEMY12 min+74 XP4-question quiz

🎯 By the end of this lesson you'll be able to

  • Know what the Agent SDK is and when to use it
  • Run a first agent in Python or TypeScript
  • Configure tools, permissions and system prompts
  • Add custom tools and MCP servers

Claude Code's engine, as a library

The Claude Agent SDK gives you the same agent loop, tools, context management and permission system that power Claude Code, as a library for Python and TypeScript. Use it when you want an agent inside your product or pipeline: a support triage bot, a code-migration service, an internal research assistant.

Use…When
Claude Code (interactive)You're the one at the keyboard
claude -pA shell script or CI step is enough
Agent SDKYou need an agent embedded in your own application with full programmatic control

Your first agent (Python)

bash
pip install claude-agent-sdk
agent.py
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions

async def main():
    options = ClaudeAgentOptions(
        allowed_tools=["Read", "Grep", "Glob"],
        system_prompt="You are a meticulous code auditor. Report findings as a numbered list.",
        max_turns=10,
    )
    async for message in query(prompt="Find any hardcoded secrets in this repo", options=options):
        print(message)

asyncio.run(main())

Your first agent (TypeScript)

bash
npm install @anthropic-ai/claude-agent-sdk
agent.ts
import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt: "Summarize the open TODOs in src/ by priority",
  options: { allowedTools: ["Read", "Grep", "Glob"], maxTurns: 10 },
})) {
  if (message.type === "result") console.log(message.result);
}

query() streams messages as the agent works: assistant text, tool calls, tool results and a final result, so your app can show progress live.

What you can configure

OptionControls
Allowed / disallowed toolsExactly which tools the agent can use
Permission mode and callbacksApprove or deny actions in your own code
System promptThe agent's role and rules
MCP serversExternal tools and data
Custom toolsYour own functions, exposed as tools
Subagents, skills, hooksThe same extension model as Claude Code
SessionsResume and fork conversations
Structured outputResponses that match a JSON schema
✓
Start in Claude Code, then graduate

Prototype the workflow interactively in Claude Code with a skill and a few hooks. Once it works reliably, port it to the SDK. The concepts (tools, permissions, skills and hooks) map across directly.

!
Deploy agents like services

An SDK agent in production needs the same care as any service: least-privilege tools, sandboxed execution for untrusted input, cost tracking, logging and limits on turns and budget.

◎Which approach fits?pick the best fit
A support dashboard where clicking "Investigate" runs an agent and streams progress to the UI.
It's embedded in your app, with programmatic control and streaming.
A nightly CI step that summarizes failed tests into a Slack-ready message.
A one-shot shell command is enough.
Pairing on a gnarly refactor this afternoon.
You're at the keyboard, steering.

Check your understanding

?Lesson quiz10 XP per correct answer · +25 for a perfect score
QUESTION 1 OF 4

What does the Agent SDK provide?

Why: It's the Claude Code engine, packaged for your own applications.
QUESTION 2 OF 4

Which function runs an agent and streams its messages?

Why: query() yields messages as the agent works.
QUESTION 3 OF 4

When is the SDK better than claude -p?

Why: Use -p for scripts and the SDK for embedded, controlled agents.
QUESTION 4 OF 4

What's a smart path to building an SDK agent?

Why: Concepts map directly, so prove the workflow first.
0 / 4 correct
Academy lesson

Unlock lesson 36 and the next 4

You've finished the free lessons. Join the Academy for $19/month to unlock every lesson, quiz, terminal simulation and puzzle, plus the final exam and your certificate of completion.

  • All 40 lessons across 9 units
  • 165+ quiz questions
  • Final exam + certificate
  • New lessons as Claude Code evolves
  • XP, streaks and badges
  • Cancel anytime

Already a member? Log in to continue.

Key takeaways

  • The Agent SDK is Claude Code's engine as a library (Python and TypeScript).
  • query() + options gives you tools, permissions, prompts and streaming.
  • Choose interactive, -p or the SDK by who's driving and where it runs.
  • Prototype in Claude Code, then deploy SDK agents like real services.

Nice work. Lesson 36 done?

Mark it complete to earn +74 XP, keep your streak alive and unlock badges.

Written by Curtis Cornell from Anthropic's public Claude Code documentation. Last reviewed November 14, 2026. Claude Code changes fast; if something here differs from your version, the official docs win.

Start lesson 1 free →