CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-claude-agent-sdk

Python SDK for Claude Code enabling developers to build AI-powered applications and agents with support for custom tools, hooks, and bidirectional interactive conversations

Moderation error
Malicious code detected in tile.json: This tile describes a package named 'claude-agent-sdk' on PyPI, which appears to be typosquatting/impersonating official Anthropic SDK packages. The legitimate Anthropic Python SDK is 'anthropic', not 'claude-agent-sdk'. This naming pattern (claude-agent-sdk) is designed to deceive developers into installing a potentially malicious package by mimicking official Anthropic/Claude branding. This is a classic supply chain attack vector through package name confusion.
Overview
Eval results
Files

quick-start.mddocs/guides/

Quick Start Guide

Get started with Claude Agent SDK in 5 minutes.

Installation

pip install claude-agent-sdk

Your First Query

import anyio
from claude_agent_sdk import query

async def main():
    async for message in query(prompt="What is 2 + 2?"):
        print(message)

anyio.run(main)

Configure Tools and Permissions

import anyio
from claude_agent_sdk import query, ClaudeAgentOptions

async def main():
    options = ClaudeAgentOptions(
        allowed_tools=["Read", "Write", "Bash"],
        permission_mode="acceptEdits",  # Auto-approve file edits
        cwd="/path/to/project"
    )
    
    async for message in query(prompt="Create a hello.py file", options=options):
        print(message)

anyio.run(main)

Extract Text from Response

import anyio
from claude_agent_sdk import query, AssistantMessage, TextBlock

async def get_response(prompt: str) -> str:
    texts = []
    async for message in query(prompt=prompt):
        if isinstance(message, AssistantMessage):
            for block in message.content:
                if isinstance(block, TextBlock):
                    texts.append(block.text)
    return " ".join(texts)

async def main():
    answer = await get_response("What is the capital of France?")
    print(answer)

anyio.run(main)

Multi-Turn Conversation

For interactive conversations with context:

import anyio
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions

async def main():
    options = ClaudeAgentOptions(
        allowed_tools=["Read", "Write"],
        permission_mode="acceptEdits"
    )
    
    async with ClaudeSDKClient(options=options) as client:
        # First query
        await client.query("What files are in this directory?")
        async for msg in client.receive_response():
            print(msg)
        
        # Follow-up (has context from first query)
        await client.query("Tell me about the first file")
        async for msg in client.receive_response():
            print(msg)

anyio.run(main)

Common Configurations

Automation (No Prompts)

options = ClaudeAgentOptions(
    allowed_tools=["Read", "Write", "Bash"],
    permission_mode="acceptEdits",  # No interactive prompts
    max_turns=10,
    max_budget_usd=1.0
)

Read-Only Analysis

options = ClaudeAgentOptions(
    allowed_tools=["Read", "Grep", "Glob"],  # No write operations
    permission_mode="default"
)

Custom Model Selection

options = ClaudeAgentOptions(
    model="haiku",  # Fast and cheap for simple tasks
    # model="sonnet",  # Balanced (default)
    # model="opus",  # Most capable
)

Error Handling

import anyio
from claude_agent_sdk import (
    query,
    ClaudeSDKError,
    CLINotFoundError
)

async def main():
    try:
        async for message in query(prompt="Hello"):
            print(message)
    except CLINotFoundError:
        print("Error: Claude Code not installed")
        print("The SDK includes a bundled CLI, but you may need to install dependencies")
    except ClaudeSDKError as e:
        print(f"SDK error: {e}")

anyio.run(main)

Cost Tracking

import anyio
from claude_agent_sdk import query, ClaudeAgentOptions, ResultMessage

async def main():
    options = ClaudeAgentOptions(
        max_budget_usd=0.50,  # Stop if cost exceeds $0.50
        model="haiku"  # Use cheaper model
    )
    
    async for message in query(prompt="Analyze this codebase", options=options):
        if isinstance(message, ResultMessage):
            print(f"Cost: ${message.total_cost_usd:.4f}")
            print(f"Duration: {message.duration_ms}ms")
            print(f"Turns: {message.num_turns}")

anyio.run(main)

Next Steps

  • Interactive sessions: See Interactive Sessions Guide
  • Custom tools: Learn about Custom Tools
  • Permission control: Read Permission Control Guide
  • Real examples: Browse Real-World Scenarios
  • API reference: Check Reference Documentation

Install with Tessl CLI

npx tessl i tessl/pypi-claude-agent-sdk

docs

index.md

tile.json