Skip to content

Day 51 of 52 · about 30 min · Automation · Bonus

Build your own agent with the Agent SDK

The loop that runs Claude Code is also a library. With it you can put an agent inside your own script, product or pipeline, and decide exactly which tools it may use.

You’ll finish with

  • A working agent script you can point at your own tasks
  • A clear picture of query, allowedTools and permissionMode

You’ll need

  • An API key from the Claude Console. Without Anthropic’s approval, products built on the Agent SDK can’t offer claude.ai login.
  • Node.js 22 or newer, the Day 51 kit and some JavaScript

Before you start

Tell us about your setup and the steps change to fit it. Your answers stay in this browser, and you can change them at any time.

Which system are you on?

Commands differ between systems. Pick the one you’ll run Claude Code on.

The steps

One step at a time. When a step’s check matches, tick it and the next one opens.

0 of 4 steps done0%
  1. Step 1 of 4: Install the SDK in the kit

    One npm package, with Claude Code bundled.

    Unzip the kit and open a terminal in its folder. The package bundles a native Claude Code binary, so most setups need nothing else.

    Terminal

    What happens: In the horizonlux-claude-code-day-51 folder:

    Run: npm install @anthropic-ai/claude-agent-sdk

    The same command works in PowerShell.

    Check it worked

    package.json now lists @anthropic-ai/claude-agent-sdk under dependencies.

  2. Step 2 of 4: Set your API key

    In the same shell that will run the agent.

    The SDK reads the key from the environment of the process that runs it. It doesn’t load .env files, and the key never goes in a committed file.

    For macOS, Linux and WSL

    Terminal

    Run: export ANTHROPIC_API_KEY=your-api-key

    Replace your-api-key with your key.

    For Windows

    PowerShell

    Run: $env:ANTHROPIC_API_KEY = "your-api-key"

    Replace your-api-key with your key.

    Check it worked

    Works in every shell
    node -e "console.log(Boolean(process.env.ANTHROPIC_API_KEY))"

    It prints true without showing the key.

  3. Step 3 of 4: Read the agent

    A prompt, two options and a loop.
    agent/fix-bugs.mjsJavaScript
    // Day 51: a small agent built with the Claude Agent SDK.
    //
    // It follows the official quickstart
    // (https://code.claude.com/docs/en/agent-sdk/quickstart), written as plain
    // JavaScript so `node` runs it without a TypeScript step.
    //
    // Run it from the project folder, after `npm install @anthropic-ai/claude-agent-sdk`:
    //   node agent/fix-bugs.mjs
    // ANTHROPIC_API_KEY must be set in the same shell. The SDK does not read .env files.
    import { query } from '@anthropic-ai/claude-agent-sdk';
    
    // The agent loop: query() streams messages while Claude works.
    for await (const message of query({
      prompt: 'Review agent/practice.js for bugs that would cause crashes. Fix any issues you find.',
      options: {
        allowedTools: ['Read', 'Edit', 'Glob'], // pre-approved: read, edit and find files
        permissionMode: 'acceptEdits', // approve file edits without asking
      },
    })) {
      // Print only the readable parts: Claude's text and the tools it calls.
      if (message.type === 'assistant' && message.message?.content) {
        for (const block of message.message.content) {
          if ('text' in block) {
            console.log(block.text);
          } else if ('name' in block) {
            console.log(`Tool: ${block.name}`);
          }
        }
      } else if (message.type === 'result') {
        console.log(`Done: ${message.subtype}`);
      }
    }
    

    query starts the agent loop and streams messages as Claude works. allowedTools pre-approves reading, finding and editing files, and acceptEdits approves edits without asking. The docs’ quickstart uses TypeScript; this is the same code as plain JavaScript.

    In order:

    1. You

      You run the script

    2. Claude

      query() starts the loop

    3. Tool

      Read, Glob and Edit

      Pre-approved, no prompts

    4. Shell

      A result message

      The script prints Done: and the result

    One run of the agent, start to finish.

    Check it worked

    You can point to the prompt, the tool list and the line that prints Done:.

  4. Step 4 of 4: Run it and check the fix

    The agent edits practice.js, then you check it.

    agent/practice.js has two functions that crash: one on an empty list, one when the client is missing.

    Terminal

    Run: node agent/fix-bugs.mjs

    What happens: Claude’s reasoning and each tool it calls print as it works.

    Output: Done: success
    A successful run ends with Done: success.

    Check it worked

    Both functions in agent/practice.js now handle the input that crashed them, and npm test still passes.

    If it didn’t work

    It fails with Not logged in or Invalid API key.

    Set ANTHROPIC_API_KEY in the shell that runs the agent, then run it again.

Finish · 4 of 4 steps to go

You built an agent

Change the prompt or point it at your own code. Adding "Bash" to allowedTools lets it run commands without asking, so try that in a throwaway folder first.

What you’ll have checked

  • The SDK is installed and your key is set
  • The agent fixed both bugs and printed Done: success
  • npm test still passes

Lesson updates by email

Get lesson updates by email

Leave your email and we’ll tell you when we add or update a lesson.

We’ll email you when we add or update a lesson. We use your email only for this. Privacy policy

Claude Code for your team

Want Claude Code set up across your team?

HorizonLux sets up and repairs Claude Code for teams: CLAUDE.md rules, hooks that enforce them and reviewed MCP servers. Book a free call to talk it through.

Sources

Checked against the official Claude Code docs on .

Independent resource, not affiliated with Anthropic. HorizonLux wrote this lesson; Claude Code is made by Anthropic.