Claude Agent SDK: a practical guide with examples

Build a small repository reviewer with the Claude Agent SDK, scoped tools, and practical JavaScript examples.

Claude Agent SDK guide with connected geometric modules illustrating an agent workflow

The Claude Agent SDK lets developers build applications around Claude’s agent loop: inspecting information, using tools, and working through a task across multiple steps. It is useful when a workflow needs more than a single generated response. This guide builds a small repository reviewer in JavaScript, then shows how to adapt it for a release-readiness report.

The SDK exposes capabilities associated with Claude Code through a programmatic interface. Anthropic provides TypeScript and Python options; the JavaScript examples below use the TypeScript package from Node.js. See the official Agent SDK overview for the supported capabilities.

When should you use the Claude Agent SDK?

A useful starting point is a task that has a clear outcome but requires several investigative steps. Reviewing a small codebase, explaining a failing feature, or preparing a documented handover can fit that pattern.

For example, a repository review might require finding route files, reading package scripts, and checking whether the README explains local setup. The agent chooses which files to inspect; your application defines the task and controls the tools available to it.

For a fixed transformation, such as changing a known JSON field, an ordinary script is usually easier to test. For one prompt and one response, consider whether a direct model API call is sufficient. Add an agent loop when exploration is part of the work, rather than making every automation an agent.

Set up a separate example project

Use a supported Node.js version and a clean folder outside a client’s production repository. Install the package in that example folder:

mkdir agent-review-demo
cd agent-review-demo
npm init -y
npm install @anthropic-ai/claude-agent-sdk

Provide ANTHROPIC_API_KEY through your shell or secret manager before running the example. Do not put the key in source code or commit it. The SDK does not automatically load a .env file, so use your runtime’s environment-loading mechanism if you keep local credentials there. The SDK quickstart covers installation and authentication requirements.

Use a small, sanitized repository copy for the first run. Remove credentials, customer exports, and private documents. API usage can incur charges; the examples here are starting points, not benchmarks or a promise of a particular cost.

Example 1: review a repository without editing files

Save the following as review.mjs. It accepts a repository path, exposes three inspection tools, and prints a successful final response. It also reports unsuccessful runs separately instead of treating every result as success.

import path from 'node:path';
import { query } from '@anthropic-ai/claude-agent-sdk';

const repository = process.argv[2];
if (!repository) {
  throw new Error('Usage: node review.mjs /path/to/sample-repository');
}
if (!process.env.ANTHROPIC_API_KEY) {
  throw new Error('Set ANTHROPIC_API_KEY in your environment first.');
}

const task = process.argv[3] ?? [
  'Review this repository without changing files.',
  'Identify the framework and available validation commands.',
  'Report up to three concrete maintenance concerns.',
  'Cite file paths for evidence and distinguish unknowns from findings.',
  'Do not claim that any tests have run.'
].join(' ');

let receivedResult = false;
try {
  for await (const message of query({
    prompt: task,
    options: {
      cwd: path.resolve(repository),
      tools: ['Read', 'Glob', 'Grep'],
      allowedTools: ['Read', 'Glob', 'Grep'],
      settingSources: [],
      maxTurns: 8
    }
  })) {
    if (message.type !== 'result') continue;
    receivedResult = true;
    if (message.subtype === 'success') {
      console.log(message.result);
    } else {
      console.error(`Review did not complete: ${message.subtype}`);
      process.exitCode = 1;
    }
  }
  if (!receivedResult) throw new Error('No final result was received.');
} catch (error) {
  console.error(error instanceof Error ? error.message : String(error));
  process.exitCode = 1;
}

Run it against your sample repository:

node review.mjs ../sample-repository

The tools option configures the available built-in tools. The allowedTools option preapproves named tools; it is not, by itself, a complete restriction on the tool set. Setting settingSources to an empty array disables filesystem settings loading, although managed policies can still apply. Check the TypeScript SDK reference when adapting these options to your installed version.

This example deliberately provides no shell or file-editing tool. That limits its job to inspection, so its report cannot prove that a build or test passed. The working directory is also not an operating-system sandbox. Use process isolation and filesystem permissions when handling untrusted projects or running a hosted service.

Example 2: create a release-readiness checklist

The third argument lets you reuse the same reviewer with a different task. This example asks for evidence and unanswered questions before a release:

node review.mjs ../sample-repository "Inspect package scripts, configuration examples, and setup documentation. Produce a release-readiness checklist with file references. Separate documented checks from checks that still need a human to run. Do not read secret files or claim a deployment is ready."

A useful report might identify a documented build command, an environment-variable example file, and a missing rollback note. Those are illustrative outcomes, not results from a real execution of this example.

Review the cited files yourself. If the task requires running tests, execute the project’s established checks separately and compare their output with the report. Keeping evidence collection separate from release approval makes mistakes easier to spot.

Permissions, limits, and failure handling

Start with the smallest tool set that can complete the task. Add editing only when you have a reason, and review the permission flow before introducing a shell. A permission callback is not a universal interception layer: earlier rules and modes can affect whether a request reaches it. The permissions documentation explains that evaluation order.

Set a turn limit and handle the case where the task stops before completion. For a production application, also define a timeout, cancellation behavior, logging policy, and a way to retry a failed job without repeating external side effects.

Avoid logging whole prompts or tool outputs by default when they might contain client data. Keep useful operational details, such as whether a job succeeded and which validation stage failed, without making logs a second copy of the repository.

How to evaluate your first agent

Create a small fixture repository with a known issue, such as a README that names a missing script. Run the reviewer several times and check whether it consistently finds the issue with an accurate file reference.

Then try a repository with no matching problem. A reviewer that invents findings is not ready to automate decisions. Evaluate false positives, missing evidence, incomplete results, and how clearly the output explains uncertainty.

For shared project instructions, see my CLAUDE.md best-practices guide and template. If you want an interactive development assistant before building your own workflow, start with installing Claude Code.

Conclusion: begin with one bounded workflow

The best first Claude Agent SDK project has one job, limited tools, and an output you can check. A repository reviewer is a practical starting point because you can inspect every cited file and compare its conclusions with known facts.

Expand from there only when the workflow is reliable. If you need help designing a development workflow or introducing Claude tooling to a team, explore my Claude Code development and consulting service or get in touch.

What I would do in a real project

I prefer a small, well-tested implementation that solves the client’s actual workflow. The technology matters, but clear ownership, publishing checks, and a maintainable handover matter more than a fashionable stack.

Common questions

How should I start evaluating this approach?

Start with one representative page or workflow, use realistic content, and test the full path from editing or development through the public visitor experience.

Is this approach right for every project?

No. Compare it with the simplest setup that meets the requirements, including the team’s editing needs, integrations, hosting, and ongoing maintenance.

What should I test before launch?

Test mobile layouts, error states, performance, accessibility, metadata, forms or checkout flows, publishing, and recovery from a failed build or integration.

When should I bring in a developer?

Bring in help when the content model, integrations, migration, testing, or deployment process has risks that the current team cannot confidently own.

Put the idea into practice

Need a bounded agent workflow for a real repository or internal process? Let’s define the tools, limits, and review path.

Explore Claude Code development ↗

Let’s connect

Currently open to collaborations

info@awais-javed.com

Open email ↗

Choose your mail app or copy the address.