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,allowedToolsandpermissionMode
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.
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.
TerminalWhat happens: In the
horizonlux-claude-code-day-51folder:Run: npm install @anthropic-ai/claude-agent-sdk
The same command works in PowerShell. Check it worked
package.jsonnow lists@anthropic-ai/claude-agent-sdkunder dependencies.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
.envfiles, and the key never goes in a committed file.For macOS, Linux and WSL
TerminalRun: export ANTHROPIC_API_KEY=your-api-key
Replace your-api-key with your key. For Windows
PowerShellRun: $env:ANTHROPIC_API_KEY = "your-api-key"
Replace your-api-key with your key. Check it worked
Works in every shellnode -e "console.log(Boolean(process.env.ANTHROPIC_API_KEY))"It prints
truewithout showing the key.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}`); } }querystarts the agent loop and streams messages as Claude works.allowedToolspre-approves reading, finding and editing files, andacceptEditsapproves edits without asking. The docs’ quickstart uses TypeScript; this is the same code as plain JavaScript.In order:
You
You run the script
Claude
query() starts the loop
Tool
Read, Glob and Edit
Pre-approved, no prompts
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:.Step 4 of 4: Run it and check the fix
The agent edits practice.js, then you check it.agent/practice.jshas two functions that crash: one on an empty list, one when the client is missing.TerminalRun: node agent/fix-bugs.mjs
What happens: Claude’s reasoning and each tool it calls print as it works.
Output: Done: successA successful run ends with Done: success. Check it worked
Both functions in
agent/practice.jsnow handle the input that crashed them, andnpm teststill passes.If it didn’t work
It fails with Not logged in or Invalid API key.
Set
ANTHROPIC_API_KEYin the shell that runs the agent, then run it again.
Finish · 4 of 4 steps to go
You built an agent
"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 teststill 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.
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.
Related
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.