BUILD · 5 MIN READ
Build with the Claude Agent SDK
Give Claude a scoped working directory and an explicit tool inventory.
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.
Resolve the real user and pass a bounded task to the SDK.
The runtime manages the loop or explicit state transitions; it does not replace resource authorization.
invokeTo 03 · Configured modelvalidatedTo 04 · Allowed toolscompleteTo 06 · Verified answerKeep provider credentials on the server and select a model supported by the API account.
decisionBack to 02 · Claude Agent SDKThe example exposes a local fixture. A real tool must check the authenticated user and resource.
observationBack to 02 · Claude Agent SDKTrack the current conversation and stop when the turn or recursion budget is reached.
Result or stopping pointFor A104, confirm that the answer reflects the lookup observation and contains no invented dates.
Result or stopping point
The runtime manages the loop or explicit state transitions; it does not replace resource authorization.
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, setANTHROPIC_API_KEY, and create asample-projectdirectory containing a few non-sensitive text files. - The SDK needs a supported platform installation.
- Save this file as
review.pyand runpython review.py.
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.txtimport 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
toolsselects the tool inventory.allowed_toolscontrols 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
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.