Setting Up and Installing the Claude Agent SDK
Initialize a Bun project, install the SDK, and write your first working agent in under 10 minutes. Covers streaming input mode, the query loop, and what each option actually does.
What you need before you start
Three things:
1. A Claude.ai subscription or an Anthropic API key. For local development, your existing Claude subscription works. For agents running in the cloud, you need an API key from console.anthropic.com. 2. Node.js. Most operators already have this installed. If not, go to nodejs.org, download the installer, and run it. 3. Bun. Bun is the recommended runtime for Agent SDK development. It is fast, requires minimal configuration, and ships with a built-in SQLite database that becomes useful once you get to session persistence.
> On models and cost: For initial development, Haiku is Anthropic's cheapest and fastest model. Use it while you are building and testing. Switch to Sonnet when you need stronger reasoning, and Opus when you need maximum capability. There is no reason to spend money on Opus while you are figuring out if your agent structure works.
Initializing your project
Open your terminal and navigate to where you want your project to live. Then:
mkdir my-agent && cd my-agent
bun init -y
bun init -y creates a new Bun project with a package.json and some starter files. You do not need to touch most of them.
Now install the Agent SDK:
bun add @anthropic-ai/claude-agent-sdk
You will see the package appear in your package.json dependencies. That is the only package you need to get started.
Writing your first agent
Rename index.ts to agent.ts — it is a clearer name for what you are building. Then paste this code:
import { query } from "@anthropic-ai/claude-agent-sdk";
async function* messages() {
yield {
role: "user" as const,
content: "Create a file called hello.txt and write a short haiku about operators.",
};
}
for await (const message of query(messages(), {
model: "claude-haiku-4-5",
allowedTools: ["write"],
permissionMode: "bypassPermissions",
dangerouslyAllowBypassPermissions: true,
})) {
if (message.type === "assistant") {
for (const block of message.message.content) {
if (block.type === "text") {
console.log(block.text);
}
}
}
}
A few things to understand about this code:
The messages function is an async generator — notice the * after function. It yields one message of type user. This is called streaming input mode, and it is the recommended way to call the SDK because it unlocks the full feature set including hooks, interrupts, and multi-turn conversations.
The for await loop processes every message the agent emits during its run. The agent does not return a single response. It streams a sequence of messages: session initialization, tool calls, text blocks, and a final result. The loop gives you access to all of them.
allowedTools: ["write"] restricts this agent to only the write tool. It cannot read files, search the web, or run bash commands. The task only needs to create a file, so that is all we give it.
permissionMode: "bypassPermissions" combined with dangerouslyAllowBypassPermissions: true means the agent will not ask for approval before using its tools. The two options work as a double opt-in — Anthropic built it this way to make sure the choice is deliberate.
Running the agent
bun agent.ts
If everything is set up correctly, you will see a hello.txt file created in your project folder, and the agent's text response logged in your terminal.
That is the entire foundation. Every agent you build from here — whether it is a Slack bot, a research pipeline, or a client-facing automation — is a variation of this same structure.
> Start folder vs end folder: If you are following along with the course repository, each lesson has a start/ folder for writing code and an end/ folder with confirmed working code. When something breaks, the end/ folder is your reference. The most common issue is running bun agent.ts from the wrong directory — always check your working directory first.
---
Author: FractionalSkill