BUILD · 5 MIN READ

Build with the Claude Agent SDK

Give Claude a scoped working directory and an explicit tool inventory.

Download example files Code + setup + checks in a ZIP
Jump to a section 9 sections

EXAMPLE USE CASE

Review a small project using read-only tools

A developer wants a project summary with file evidence and no automated edits.

Input
A sample-project directory containing README.txt and a request to summarize its purpose.
Expected output
A short summary naming the files used, plus the SDK terminal outcome.
  • Success check: The result describes the fixture and cites README.txt; the tool inventory is Read, Glob and Grep.
  • Try a failure: Remove the fixture file. The agent should report missing evidence rather than invent project behavior.

Example scope: A real Claude Agent SDK project-reading example. Tool inventory and approval settings are separate from process isolation.

Claude Agent SDK exampleDocker example
SEE HOW THE SYSTEM FITS TOGETHERSELECT A COMPONENT
taskinvokedecisionvalidatedobservationstatecompleteApplication requestIdentity + goalClaude Agent SDKAgent or graph runtimeConfigured modelAvailable provider modelAllowed toolsTyped application functionsState + limitsMessages and stopping rulesVerified answerCompare with the result
  1. Resolve the real user and pass a bounded task to the SDK.

  2. The runtime manages the loop or explicit state transitions; it does not replace resource authorization.

  3. Keep provider credentials on the server and select a model supported by the API account.

  4. The example exposes a local fixture. A real tool must check the authenticated user and resource.

  5. Track the current conversation and stop when the turn or recursion budget is reached.

    Result or stopping point
  6. For A104, confirm that the answer reflects the lookup observation and contains no invented dates.

    Result or stopping point
Claude Agent SDK

The runtime manages the loop or explicit state transitions; it does not replace resource authorization.

Follow the arrows, then select a component to inspect its responsibility. On mobile, downward arrows connect the next step; bent arrows identify branches and return paths. Select a destination to jump to that component.

Where it fits

  • The Claude Agent SDK exposes an agent loop with built-in capabilities for working with a local environment.
  • This example reads a sample project and asks for a short summary.
  • Tool configuration limits the available inventory to reading and searching.

Prepare the example

  • Install claude-agent-sdk, set ANTHROPIC_API_KEY, and create a sample-project directory containing a few non-sensitive text files.
  • The SDK needs a supported platform installation.
  • Save this file as review.py and run python review.py.
Shell · install in a virtual environment
python3 -m venv .venv
source .venv/bin/activate
python -m pip install claude-agent-sdk
export ANTHROPIC_API_KEY="your-api-key"
mkdir -p sample-project
printf "A tiny order-status service.\n" > sample-project/README.txt
Python · review.py
import asyncio
from claude_agent_sdk import (
    query, ClaudeAgentOptions, ResultMessage
)

async def main():
    options = ClaudeAgentOptions(
        cwd="./sample-project",
        tools=["Read", "Glob", "Grep"],
        allowed_tools=["Read", "Glob", "Grep"],
        max_turns=5,
    )
    async for message in query(
        prompt=(
            "Read the text files in this project. "
            "Summarize its purpose and name the files you used."
        ),
        options=options,
    ):
        if isinstance(message, ResultMessage):
            print("Outcome:", message.subtype)
            print(message.result or "No final text returned.")

if __name__ == "__main__":
    asyncio.run(main())

Educational example. Uses your configured environment and credentials; no paid model call is made by this website.

Separate inventory from approval

  • tools selects the tool inventory. allowed_tools controls automatic approval for those tools.
  • These settings serve different purposes.
  • A dedicated runtime or container supplies the isolation boundary for a real deployment.

Inspect the outcome

  • Consume the message stream and check the result subtype.
  • A turn limit can stop the run before the task is complete.
  • Treat missing files and incomplete summaries as separate outcomes rather than assuming every final message means success.

Run and verify

Shell
python review.py
  • The result names README.txt and describes the fixture.
  • The available tools are Read, Glob, and Grep; the sample supplies no write or shell tool.

Troubleshooting and failure tests

  • A missing working directory fails before useful file analysis; create sample-project first.
  • Inspect ResultMessage.subtype as well as result text. A turn limit or tool failure is not a successful review.

Before production

  • Replace the local fixture with a service that enforces authenticated tenant and resource permissions.
  • Add timeouts, bounded retries, tracing redaction, and a regression case for each observed failure.
  • Model calls consume provider API usage when you run this example; this website only displays the code.

Keep exploring

A browser or coding agent in a sandboxDesign tools an agent can use well