Install & First Query

Lesson Content

Setting Up Your Environment

Setup in 3 Steps
01Node.jsv18+ installed
The workbench
02API KeyANTHROPIC_API_KEY
Your access badge
03SDKnpm install
The toolbox
All three before you can build anything.

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.

Real-world analogy: Setting up the SDK is like setting up a workshop. Node.js is the workbench. The API key is your badge that gets you into the building. The SDK package is the toolbox. You need all three before you can build anything.

Step 1: Install Node.js

The Agent SDK runs on Node.js 18 or later. Check if you already have it:

Terminal — check your Node.js version
# 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.

1
Go to console.anthropic.com and sign in (or create an account)
2
Navigate to Settings > API Keys and click "Create Key"
3
Set it as an environment variable so the SDK can find it automatically
Terminal — set your API key
# Add to your shell profile (~/.zshrc or ~/.bashrc)
export ANTHROPIC_API_KEY="sk-ant-api03-your-key-here"

# Reload your shell
source ~/.zshrc
Security: Never put your API key directly in source code. Never commit it to Git. Use environment variables or a .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:

Terminal — create project and install
# 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.

TypeScript — first-agent.ts
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
Terminal — run your first agent
# 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:

TypeScript — agent with file tools
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:

TypeScript — inspecting the result
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

"ANTHROPIC_API_KEY is not set"

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.

"Cannot find module @anthropic-ai/claude-agent"

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.

"Permission denied" when using file tools

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?
Node.js 18 or later. Check with: node --version. Install from nodejs.org if needed.
How does the SDK find your API key?
It reads the ANTHROPIC_API_KEY environment variable automatically. Set it with: export ANTHROPIC_API_KEY="sk-ant-..." in your shell profile.
What does tools: "defaults" give you?
The defaults preset enables built-in tools: Bash (terminal commands), Read/Write/Edit (file operations), and Grep/Glob (search). These let the agent interact with your local environment.
What does query() return?
A result object containing: text (Claude response), cost (USD spent), toolCalls (number of tool invocations), sessionId (for resuming later), and other metadata.
What happens inside a query() call when tools are enabled?
The SDK runs an automatic tool loop: Claude decides to use a tool, the SDK executes it, sends results back to Claude, and repeats until Claude has enough information to respond. All transparent to you.
What is the difference between query() with and without tools?
Without tools, query() is essentially an API call — Claude can only think and respond. With tools, Claude can read files, run commands, search code, and take real actions during the query.

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?