Skip to content

Capx-AI/claude-agent-sdk-examples

Repository files navigation

The Missing Manual: Claude Agent SDK

Claude Agent SDK Examples

Working examples of the patterns from the tweet thread


What is this?

Basic examples showing how to use the Claude Agent SDK with different authentication methods and sandbox providers.

The 4 patterns:

  1. Using OAuth - Bring your Claude Max subscription ($20/month)
  2. Swap Anthropic for other providers - Point to any Anthropic-compatible endpoint
  3. Sandboxed Execution - Isolated environments for multi-user apps (E2B or Daytona)
  4. Session Continuity - Resume conversations across multiple runs

Note

These examples are intended as a starting point for your own applications. They demonstrate core patterns that you can extend and build upon to create specialized agents for your specific needs.


Who Needs This?

Claude Agent SDK is powerful but needs proper setup. These examples help you:

  • Hackathon Hackers - Ship your idea fast without fighting setup
  • Indie Builders - Start building with the Claude Agent SDK immediately
  • Tinkerers - Experiment with powerful agentic workflows quickly
  • Anyone building AI apps - See the Claude Agent SDK in action, not just docs

Quick Starts

Note: These are just a few examples. You can use these patterns for countless other use cases.

Choose your path:

Option 1: Fast Sandboxes (E2B) + OAuth

Example use case: Quick agent tasks with your Claude Max subscription

# 1. Clone and install
git clone https://github.com/capx-ai/claude-agent-sdk-examples.git
cd claude-agent-sdk-examples
npm install

# 2. Get API keys
# - E2B: https://e2b.dev (free tier available)
# - OAuth: Run `claude setup-token` (requires Claude Max)

# 3. Add to .env
cp .env.example .env
# Add:
E2B_API_KEY=e2b_xxxxx
CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-xxxxx

# 4. Run
npm run e2b

What happens: Agent runs in isolated E2B sandbox (~2s startup), uses your Claude Max plan.


Option 2: Fast Sandboxes (E2B) + AI Gateway

Example use case: Swap Anthropic for other providers via AI Gateway

# 1. Clone and install (same as above)
git clone https://github.com/capx-ai/claude-agent-sdk-examples.git
cd claude-agent-sdk-examples
npm install

# 2. Get API keys
# - E2B: https://e2b.dev
# - Vercel Gateway: https://vercel.com/dashboard

# 3. Add to .env
cp .env.example .env
# Add:
E2B_API_KEY=e2b_xxxxx
VERCEL_AI_GATEWAY_KEY=vck_xxxxx

# 4. Run
npm run e2b

What happens: The SDK isn't actually "Claude-only". You can point it to any Anthropic-compatible endpoint.

This example routes through Vercel AI Gateway to models like:

  • kat-coder-pro (specialized coding model)
  • GLM-4.7 (Chinese model with strong performance)
  • MiniMax (another powerful alternative)

Your agent code doesn't change. Just swap models via env vars.

See lib/config.ts lines 40-42 to customize models.


Option 3: Persistent Sandboxes (Daytona) + OAuth

Example use case: Long-running tasks that need state preservation

# 1. Clone and install
git clone https://github.com/capx-ai/claude-agent-sdk-examples.git
cd claude-agent-sdk-examples
npm install

# 2. Get API keys
# - Daytona: https://daytona.io (persistent dev environments)
# - OAuth: Run `claude setup-token` (requires Claude Max)

# 3. Add to .env
cp .env.example .env
# Add:
DAYTONA_API_KEY=dtn_xxxxx
CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-xxxxx

# 4. Run
npm run daytona

What happens: Agent runs in persistent Daytona sandbox, state preserved between runs.


Option 4: Persistent Sandboxes (Daytona) + AI Gateway

Example use case: Long-running tasks with alternative providers

# 1. Clone and install (same as above)

# 2. Get API keys
# - Daytona: https://daytona.io
# - Vercel Gateway: https://vercel.com/dashboard

# 3. Add to .env
cp .env.example .env
# Add:
DAYTONA_API_KEY=dtn_xxxxx
VERCEL_AI_GATEWAY_KEY=vck_xxxxx

# 4. Run
npm run daytona

What happens: Persistent sandbox + swap providers via AI Gateway (same pattern as Option 2).


Option 5: Multi-Turn Chat

Example use case: Interactive conversation with the agent

# 1. Setup (use any auth method above)

# 2. Run interactive mode
npm run interactive

# 3. Chat naturally
> What files are in this directory?
> Read the README
> Summarize it for me
> (Ctrl+C to exit)

What happens: Sandbox stays alive, agent remembers context across turns.


Option 6: Browser Automation

Example use case: Web scraping, UI testing, navigation - or anything else you need

# 1. Setup (use any auth method above)

# 2. Run browser automation
npm run mcp

# Or custom:
npx tsx mcp-playwright.ts "Go to example.com and extract the main heading"

What happens: Agent uses Playwright MCP server to control a browser.

Note: This is just one example - you can extend this for countless browser-based tasks.


Examples Overview

File What it does Sandbox Auth
e2b.ts Fast agent tasks E2B (~2s startup) OAuth or Gateway
daytona.ts Long-running tasks Daytona (persistent) OAuth or Gateway
interactive-e2b.ts Multi-turn chat E2B (long-lived) OAuth or Gateway
mcp-playwright.ts Browser automation None (runs locally) OAuth or Gateway

All examples support:

  • ✅ Session continuity (resume conversations)
  • ✅ Cost tracking (see spend after each run)
  • ✅ Error handling (helpful messages)
  • ✅ Both auth methods (OAuth or Gateway)

File Structure

claude-agent-sdk-examples/
├── README.md              # You are here
├── .env.example           # Template for API keys
│
├── Examples (run these):
│   ├── e2b.ts             # Fast sandboxes
│   ├── daytona.ts         # Persistent sandboxes
│   ├── interactive-e2b.ts # Multi-turn chat
│   └── mcp-playwright.ts  # Browser automation
│
├── Core Agent:
│   └── agent.ts           # The agent script (uploaded to sandboxes)
│
└── Library:
    ├── lib/config.ts      # Auth detection, validation
    ├── lib/types.ts       # TypeScript interfaces
    ├── lib/e2b.ts         # E2B sandbox runner
    ├── lib/daytona.ts     # Daytona sandbox runner
    └── lib/index.ts       # Barrel exports

TLDR:

  • Run examples = e2b.ts, daytona.ts, interactive-e2b.ts, mcp-playwright.ts
  • Core logic = agent.ts (what runs inside sandboxes)
  • Library code = lib/ (reusable sandbox runners)

Session Continuity (Resume Conversations)

All examples support resuming conversations. Here's how:

First run:

$ npx tsx e2b.ts "What is 7+7?"

# Output:
[session: abc-123-xyz]
7 + 7 = 14

Session ID: abc-123-xyz
Sandbox ID: sbx-456-def

Resume:

$ npx tsx e2b.ts "What was my question?" \
  --resume abc-123-xyz \
  --sandbox sbx-456-def

# Output:
Your question was "What is 7+7?"

Why both IDs?

  • --resume SESSION_ID → Tells Claude to continue the conversation
  • --sandbox SANDBOX_ID → Reconnects to the same VM (where session data lives)

Learn More

Official Documentation

Sandbox Providers

AI Gateway

MCP (Model Context Protocol)


Building Something Cool?

If you're using these patterns to build interesting AI agents, we'd love to hear about it!

  • Share on Twitter - Tag @0xcapx and @divyaranjan_
  • Open an Issue - Show us what you built!
  • Contribute - PRs welcome for new examples or improvements

About Capx

Capx is a decentralized protocol powering the AI builder economy by enabling solopreneurs to build, ship, host, and scale AI apps, as well as tokenize and launch them on Capx.


Crafted with ❤️ at Capx

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors