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.

3 min read
Claude Agent SDK install bun agent SDK setup Claude SDK first agent TypeScript query function setup

What you need before you start

Three things:

  1. A Claude.ai subscription or an Anthropic API key. For local development on your own machine, your existing Claude subscription works. For agents you deploy to run in the cloud without you present, 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 built-in SQLite support that becomes useful once you build agents that need to remember things between runs.
On models and cost during development: Use Haiku while you are building and testing. It is Anthropic's cheapest and fastest model. The agent logic you are testing — tool calls, session handling, workflow structure — works the same on Haiku as it does on Sonnet. Switch to Sonnet when you are testing output quality against a real deliverable. No reason to pay for Opus until you know your agent works.

Initializing your project

Open your terminal and navigate to where you want your project to live. A dedicated folder per agent keeps things clean:

mkdir client-brief-agent && cd client-brief-agent
bun init -y

bun init -y creates a new Bun project with a package.json and starter files. You do not need to touch most of them.

Install the Agent SDK:

bun add @anthropic-ai/claude-agent-sdk

That is the only package you need to get started. No framework, no config files, no boilerplate.

Writing your first agent

Rename index.ts to agent.ts. Then paste this code:

import { query } from "@anthropic-ai/claude-agent-sdk";

async function* messages() {
  yield {
    role: "user" as const,
    content: "Read the file client-notes.txt and write a 3-bullet summary to summary.txt.",
  };
}

for await (const message of query(messages(), {
  model: "claude-haiku-4-5",
  allowedTools: ["read", "write"],
  permissionMode: "bypassPermissions",
  dangerouslyAllowBypassPermissions: true,
})) {
  if (message.type === "assistant") {
    for (const block of message.message.content) {
      if (block.type === "text") {
        console.log(block.text);
      }
    }
  }
}

This agent reads a file and writes a structured summary — the same pattern you will use for pre-call research, weekly client briefs, and competitive snapshots. The task is placeholder, but the structure is real.

A few things to understand about this code:

The messages function is an async generator — notice the * after function. It yields a user message. This streaming input mode is the recommended way to call the SDK because it unlocks the full feature set: 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, tool results, and a final result. The loop gives you access to all of them.

allowedTools: ["read", "write"] restricts the agent to file reading and writing. It cannot search the web, run shell commands, or do anything else. The task only needs two tools, so that is all it gets. Scoping tools tightly is good practice — it makes agent behavior predictable and easier to debug.

permissionMode: "bypassPermissions" combined with dangerouslyAllowBypassPermissions: true means the agent acts without asking for confirmation before each tool use. The two options are a deliberate double opt-in. Anthropic built it this way to make the permission bypass explicit, not accidental.

Running the agent

Create a test file first:

echo "Client: Acme Corp. Q3 revenue down 12%. New CMO starting Monday. Board wants growth plan." > client-notes.txt

Then run:

bun agent.ts

The agent reads the notes and writes a structured summary to summary.txt. That is the entire foundation. Every agent you build — a pre-call research package, a multi-client status sweep, a competitive analysis pipeline — is a variation of this same structure with a different prompt, different tools, and a different output destination.

Start folder vs end folder: If you are following the course repository, each lesson has a start/ folder where you write code and an end/ folder with confirmed working code. The most common setup issue is running bun agent.ts from the wrong directory. Always check your working directory with pwd before debugging anything else.

---

Author: FractionalSkill

Keep Going

Ready to Start Building?

Pick the next step that matches where you are right now.

Tutorial
Claude Code Basics

Start with the terminal basics. A hands-on, step-by-step guide to your first 10 minutes with Claude Code.

Start the Tutorial
Guide
AI-Powered Workflows

Automate your client work. Learn how to connect AI tools into workflows that handle repetitive tasks for you.

Read the Guide
Community
Join the Community

Connect with other fractional leaders building with AI. Share workflows, get feedback, and learn from operators who are ahead of you.

Apply to Join