Setting Up Your Environment
The workbench
Your access badge
The toolbox
Before you write a single line of agent code, you need three things: Node.js, an Anthropic API key, and the SDK package. If you have ever set up a JavaScript project, this will feel familiar. If you have not, follow every step — we will not skip anything.
Step 1: Install Node.js
The Agent SDK runs on Node.js 18 or later. Check if you already have it:
# Check if Node.js is installed and what version
node --version
# You need v18.0.0 or higher
# If you see "command not found" or a version below 18,
# install from https://nodejs.org (use the LTS version)
Step 2: Get Your API Key
You need an Anthropic API key. If you already have one from using the Claude API, it works with the SDK too — same key, same account.
# Add to your shell profile (~/.zshrc or ~/.bashrc)
export ANTHROPIC_API_KEY="sk-ant-api03-your-key-here"
# Reload your shell
source ~/.zshrc
.env file (and add .env to your .gitignore). If you accidentally expose a key, rotate it immediately in the Console.
Step 3: Install the SDK
Create a new project and install the Claude Agent SDK:
# Create a new directory for your agent project
mkdir my-first-agent && cd my-first-agent
# Initialize a new Node.js project
npm init -y
# Install the Claude Agent SDK
npm install @anthropic-ai/claude-agent
# If you are using TypeScript (recommended):
npm install typescript tsx --save-dev
Your First Query
The most basic SDK operation is query(). It sends a prompt to Claude and returns a result. Unlike a raw API call, the agent can use tools during query execution — reading files, running commands, whatever it needs to answer your question.
import { Claude } from "@anthropic-ai/claude-agent";
// Create an agent instance
// It reads ANTHROPIC_API_KEY from your environment automatically
const agent = new Claude({
model: "claude-sonnet-4-6", // the model powering your agent
});
// Send a simple query — no tools needed for this one
const result = await agent.query(
"What are the three most important things to know about building AI agents?"
);
// The result object contains the response text and metadata
console.log(result.text);
// Claude responds with a thoughtful answer about agent architecture
# Run with tsx (TypeScript executor)
npx tsx first-agent.ts
Adding Tools: An Agent That Reads Files
A query without tools is just a fancy API call. The real power starts when you give your agent tools. The tools option tells Claude what it can interact with:
import { Claude } from "@anthropic-ai/claude-agent";
const agent = new Claude({
model: "claude-sonnet-4-6",
tools: "defaults", // enables Bash, Read, Write, Edit, Grep, Glob
});
// Now Claude can actually read your file system
const result = await agent.query(
"Read the package.json in this directory and list all dependencies."
);
console.log(result.text);
// Claude uses the Read tool to open package.json,
// parses the JSON, and gives you a clean summary
Behind the scenes, the SDK runs a tool loop: Claude decides to use the Read tool, the SDK executes it, sends the file contents back to Claude, and Claude formulates a response. All of that happens inside the single query() call.
Understanding the Result Object
The query() method returns a result object with more than just text:
const result = await agent.query("What files are in this directory?");
// The text response from Claude
console.log(result.text);
// How much it cost (in USD)
console.log(result.cost); // e.g., 0.0042
// How many tool calls were made
console.log(result.toolCalls); // e.g., 2
// The session ID (for resuming later)
console.log(result.sessionId); // e.g., "sess_abc123"
Common Pitfalls
You forgot to export the environment variable, or you are in a different terminal session. Run echo $ANTHROPIC_API_KEY to check. If it is empty, re-export it or add it to your shell profile.
You did not run npm install in the correct directory, or you are running your script from a different folder. Make sure your terminal is in the project root where package.json lives.
The SDK respects file system permissions. If Claude tries to read a file you do not have access to, it will fail. Run the agent with appropriate permissions for the directories you want it to access.
Install & First Query
What Node.js version does the SDK require?
How does the SDK find your API key?
What does tools: "defaults" give you?
What does query() return?
What happens inside a query() call when tools are enabled?
What is the difference between query() with and without tools?
Install & First Query Check
1Where should you store your Anthropic API key?
2What does the tools: "defaults" option enable?
3You run your agent script and get: "Cannot find module @anthropic-ai/claude-agent". What is the most likely cause?
4What information does the result object from query() contain?
5An agent with tools enabled receives the prompt "Read package.json". What happens internally?