1:1 mentoring with Big Tech AI engineers
LLM & Agentic

Your First Agent SDK Agent

Run a Claude Agent SDK agent in ten lines: query() vs ClaudeSDKClient, every message type the stream yields, and configuring model, budget and tool restrictions through ClaudeAgentOptions.

Last updated

Core4 min readFirst readWho Owns the Loop

After this section you can

  • Run a Claude Agent SDK agent and read every message type it yields
  • Configure model, budget, turn limit and tool restrictions through ClaudeAgentOptions
  • Choose between query() and ClaudeSDKClient
  • Restrict tools with disallowed_tools rather than mistaking allowed_tools for a filter
34

Your First Agent SDK Agent

Ten lines gets you an agent with a filesystem, a shell and a search tool already attached. The work is not building the loop — it is deciding what to let it touch.

THE CENTRAL IDEA

The Agent SDK is Claude Code as a library. You do not supply a loop, tools, or context management — those arrive built in. Your job starts at the options object: which tools, which permissions, which budget.

Install and run

# the Claude Code CLI ships bundled — nothing else to install
pip install claude-agent-sdk
import anyio
from claude_agent_sdk import query, AssistantMessage, TextBlock

async def main():
    prompt = "What does config.py do? Read it first."
    async for message in query(prompt=prompt):
        if isinstance(message, AssistantMessage):
            for block in message.content:
                if isinstance(block, TextBlock):
                    print(block.text)

anyio.run(main)

That agent can already read files and run commands in the working directory. It is a real agent on the first run, which is the part worth being careful about.

query() yields the whole loop — every turn, not just the answer
SystemMessage session id, model, tools available AssistantMessage ToolUseBlock “read config.py” UserMessage ToolResultBlock the harness ran it AssistantMessage TextBlock the actual answer ResultMessage total_cost_usd duration, usage, turns ▲ the only one a user sees ▲ log this one, always You did not write the tool call, the execution, or the loop — the harness owns all three. What you get is the transcript. Filtering to AssistantMessage/TextBlock is the “just give me the answer” path; keeping ResultMessage is how you learn what it cost. A run with no tools available still emits this shape — the middle two boxes simply do not appear.

Related

More in LLM & Agentic

Get full access to all 74+ sections with code examples, diagrams, and interactive animations.

Unlock Premium