CLAUDE ENGINEERING · LESSON 210 OF 278

Claude Agent SDK in Python

Build a custom agent using Claude Code's agent loop, tools, sessions, permissions, and context management from Python.

What you will learn

Build a custom agent using Claude Code's agent loop, tools, sessions, permissions, and context management from Python.

In simple words

The Claude Agent SDK exposes the same agentic foundations programmatically in Python and TypeScript. Applications can manage sessions, tools, hooks, permissions, MCP, structured output, and custom orchestration.

Visual explanation: Claude Agent SDK in Python

Trace how Application becomes Programmable agent through agent sdk.

WATCH THE IDEA

Claude Agent SDK in Python

Playing
Agentdecides nextApplicationAgent SDKProgrammable agent
  1. 1Observe: Application
  2. 2Transformation: Agent SDK
  3. 3Outcome: Programmable agent

Observe: Application. Trace how Application becomes Programmable agent through agent sdk.

The diagram highlights Application, Agent SDK, Programmable agent. Use the labels to trace the main relationship from left to right.

Custom application around the Claude agent loopPython application code configures the Agent SDK, sends a task, and consumes the resulting agent messages.AGENT / HOSTPROTOCOLSTATE + CONTRACTAUTH + HANDOFFTOOL / AGENT
Custom application around the Claude agent loopPython application code configures the Agent SDK, sends a task, and consumes the resulting agent messages.

Real-world example: Operational AI decision

A team applies claude agent sdk in python to a real business workflow.

What the team decides: The Agent SDK is the bridge from an interactive coding agent to a productized custom agent.

Risk to avoid: A strong technical concept still needs measurement, safeguards, and ownership in production.

Technical explanation

The Claude Agent SDK exposes the same agentic foundations programmatically in Python and TypeScript. Applications can manage sessions, tools, hooks, permissions, MCP, structured output, and custom orchestration.

Python and implementation examples

Use these examples to connect the concept to working code. Package-based examples assume the named library is installed in your own environment.

One-off Agent SDK task in Python

python
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions

async def main():
    options = ClaudeAgentOptions(
        system_prompt="You are an expert Python developer",
        permission_mode="acceptEdits",
    )

    async for message in query(
        prompt="Create a small Python web server and explain the changes",
        options=options,
    ):
        print(message)

asyncio.run(main())

The SDK exposes Claude Code's agent loop programmatically. Use restrictive permissions appropriate to your environment.

Common misconception

This concept is not a guarantee of a correct outcome by itself. Its value depends on suitable evidence, careful implementation, and evaluation against the real task.

QUICK QUIZ

When is the Agent SDK more appropriate than the interactive Claude Code CLI?

Practical application

The Agent SDK is the bridge from an interactive coding agent to a productized custom agent.

Key takeaway

The Agent SDK is the bridge from an interactive coding agent to a productized custom agent.

Next concept

Continue with Agent SDK Tools, Hooks, MCP, and Permissions.

Engineering view

The Agent SDK is the bridge from an interactive coding agent to a productized custom agent.

Internals view

Build a custom agent using Claude Code's agent loop, tools, sessions, permissions, and context management from Python. The Claude Agent SDK exposes the same agentic foundations programmatically in Python and TypeScript. Applications can manage sessions, tools, hooks, permissions, MCP, structured output, and custom orchestration.

Architecture view

Consider the Application → Agent SDK → Programmable agent flow, and make the system's inputs, checks, and outcomes explicit.

Was this lesson helpful?

Lesson discussion

Comments are reviewed before publication. Keep discussion focused on learning and do not post private information.

Loading discussion…

Your quiz answer and current lesson are saved on this device. Sign in to explore account-backed learning features.

Content version 1 · Last reviewed 2026-10-16