- Set up a project with the Agent SDK
- Create a file with some buggy code
- Run an agent that finds and fixes the bugs automatically
Prerequisites
- Node.js 18+ or Python 3.10+
- An Anthropic account (sign up here)
Setup
Create a project folder
Create a new directory for this quickstart:For your own projects, you can run the SDK from any folder; it will have access to files in that directory and its subdirectories by default.
Install the SDK
Install the Agent SDK package for your language:Setting
- TypeScript (new project)
- TypeScript (existing project)
- Python (uv)
- Python (pip)
"type": "module" in package.json lets your agent script use top-level await, and tsx runs TypeScript files directly.The TypeScript SDK bundles a native Claude Code binary for your platform as an optional dependency, so you don’t need to install Claude Code separately.
Set your API key
Get an API key from the Claude Console, then set it as an environment variable in the shell where you’ll run your agent:The SDK reads the key from the environment of the process that runs your agent; it doesn’t load
- macOS / Linux
- Windows (PowerShell)
.env files automatically. If you keep the key in a .env file, load it yourself, for example with the dotenv package, before calling the SDK.The SDK also supports authentication via third-party API providers:- Amazon Bedrock: set
CLAUDE_CODE_USE_BEDROCK=1environment variable and configure AWS credentials - Claude Platform on AWS: set
CLAUDE_CODE_USE_ANTHROPIC_AWS=1andANTHROPIC_AWS_WORKSPACE_ID, then configure AWS credentials - Google Cloud’s Agent Platform: set
CLAUDE_CODE_USE_VERTEX=1environment variable and configure Google Cloud credentials - Microsoft Azure: set
CLAUDE_CODE_USE_FOUNDRY=1environment variable and configure Azure credentials
Unless previously approved, Anthropic does not allow third party developers to offer claude.ai login or rate limits for their products, including agents built on the Claude Agent SDK. Please use the API key authentication methods described in this document instead.
Create a buggy file
This quickstart walks you through building an agent that can find and fix bugs in code. First, you need a file with some intentional bugs for the agent to fix. Createutils.py in the my-agent directory and paste the following code:
calculate_average([])crashes with division by zeroget_user_name(None)crashes with a TypeError
Build an agent that finds and fixes bugs
Createagent.py if you’re using the Python SDK, or agent.ts for TypeScript. Use agent.mts instead if your existing project uses CommonJS:
-
query: the main entry point that creates the agentic loop. It returns an async iterator, so you useasync forto stream messages as Claude works. See the full API in the Python or TypeScript SDK reference. -
prompt: what you want Claude to do. Claude figures out which tools to use based on the task. -
options: configuration for the agent. This example usesallowedToolsto pre-approveRead,Edit, andGlob, andpermissionMode: "acceptEdits"to auto-approve file changes. Other options includesystemPrompt,mcpServers, and more. See all options for Python or TypeScript.
async for loop keeps running as Claude thinks, calls tools, observes results, and decides what to do next. Each iteration yields a message: Claude’s reasoning, a tool call, a tool result, or the final outcome. The SDK handles the orchestration (tool execution, context management, retries) so you just consume the stream. The loop ends when Claude finishes the task or hits an error.
The message handling inside the loop filters for human-readable output. Without filtering, you’d see raw message objects including system initialization and internal state, which is useful for debugging but noisy otherwise.
This example uses streaming to show progress in real-time. If you don’t need live output (e.g., for background jobs or CI pipelines), you can collect all messages at once. See Streaming vs. single-turn mode for details.
Run your agent
Your agent is ready. Run it with the following command:- TypeScript
- Python (uv)
- Python (pip)
agent.mts, run npx tsx agent.mts instead.Done: success. After running, check utils.py. You’ll see defensive code handling empty lists and null users. Your agent autonomously:
- Read
utils.pyto understand the code - Analyzed the logic and identified edge cases that would crash
- Edited the file to add proper error handling
If you see “API key not found”, make sure you’ve set the
ANTHROPIC_API_KEY environment variable in the shell where you run your agent. The SDK doesn’t load .env files automatically. See the full troubleshooting guide for more help.Try other prompts
Now that your agent is set up, try some different prompts:"Add docstrings to all functions in utils.py""Add type hints to all functions in utils.py""Create a README.md documenting the functions in utils.py"
Customize your agent
You can modify your agent’s behavior by changing the options. Here are a few examples: Add web search capability:Bash enabled, try: "Write unit tests for utils.py, run them, and fix any failures"
Key concepts
Tools control what your agent can do:| Tools | What the agent can do |
|---|---|
Read, Glob, Grep | Read-only analysis |
Read, Edit, Glob | Analyze and modify code |
Read, Edit, Bash, Glob, Grep | Full automation |
| Mode | Behavior | Use case |
|---|---|---|
acceptEdits | Auto-approves file edits and common filesystem commands, asks for other actions | Trusted development workflows |
plan | Runs read-only tools; file edits are never auto-approved and reach your canUseTool callback | Scoping a task before approving execution |
dontAsk | Denies anything not in allowedTools | Locked-down headless agents |
auto (TypeScript only) | A model classifier approves or denies each tool call | Autonomous agents with safety guardrails |
bypassPermissions | Runs every tool without prompting, unless an explicit ask rule matches | Sandboxed CI, fully trusted environments |
default | Requires a canUseTool callback to handle approval | Custom approval flows |
acceptEdits mode, which auto-approves file operations so the agent can run without interactive prompts. If you want to prompt users for approval, use default mode and provide a canUseTool callback that collects user input. For more control, see Permissions.
Next steps
Now that you’ve created your first agent, learn how to extend its capabilities and tailor it to your use case:- Permissions: control what your agent can do and when it needs approval
- Hooks: run custom code before or after tool calls
- Sessions: build multi-turn agents that maintain context
- MCP servers: connect to databases, browsers, APIs, and other external systems
- Hosting: deploy agents to Docker, cloud, and CI/CD
- Example agents: see complete examples: email assistant, research agent, and more