Skip to content

Build a Coding Agent Using Claude Agent SDK and Daytona

View as Markdown

This guide demonstrates how to run an autonomous coding agent based on Claude Code inside a Daytona sandbox environment. The agent uses the Claude Agent SDK to follow user prompts.

The agent can develop full-stack web apps, write code in any language, install dependencies, and run scripts. It can also start and manage dev servers, and generate preview links for live apps.


1. Workflow Overview

When you launch the main module, a Daytona sandbox is created and a Python agent is initialized inside it. The agent is based on the Claude Agent SDK.

You interact with the main program via a command line chat interface. The program sends your prompts to the agent inside the sandbox, which executes them and returns the results:

Click to view example workflow
$ npm run start 174s
Creating sandbox...
Installing Agent SDK...
Initializing Agent SDK...
Press Ctrl+C at any time to exit.
User: Build a Zelda-like game where I can move around the screen and talk to famous programmers
Thinking...
I'll build a Zelda-like game for you! This will be a fun project with player movement and NPC interactions with famous programmers.
๐Ÿ”จ Write
๐Ÿ”จ Write
Now let me start a simple HTTP server to host the game:
๐Ÿ”จ Bash
Perfect! I've created a Zelda-like game called "Programmer's Quest" for you! ๐ŸŽฎ
## Game Features:
โœจ Zelda-style gameplay:
- Top-down 2D view with classic retro aesthetics
- Player character with sword and shield
- Grid-based movement system
- Environmental obstacles (trees and rocks)
๐Ÿ‘ฅ Famous Programmers as NPCs:
1. Linus Torvalds - Creator of Linux
2. Grace Hopper - COBOL pioneer and Admiral
3. Alan Turing - Father of computer science
4. Ada Lovelace - First computer programmer
5. Dennis Ritchie - Creator of C and UNIX
๐ŸŽฎ Controls:
- Arrow Keys or WASD - Move your character
- SPACE - Talk to NPCs when you're near them
๐ŸŒŸ Gameplay:
- Explore the grassy map and find all 5 legendary programmers
- Each NPC has multiple quotes that cycle when you talk to them
- NPCs glow when you're near them
- Dialog boxes appear with their famous quotes
- Track your progress in the HUD
## Play Now:
๐ŸŽฏ [Click here to play the game!](https://80-8e2c4d23-212a-4f1e-bb6c-abfa71aeed3a.proxy.daytona.works)
The game features smooth movement, collision detection with trees and rocks, and an immersive dialog system. Try to find and talk to all 5 famous programmers to learn their wisdom! Each has 3 different quotes that cycle as you keep talking to them.
Enjoy your adventure! ๐Ÿ—ก๏ธโœจ
User:

The agent can also host web apps and provide you with a preview link using the Daytona Preview Links feature. When your task involves running or previewing a web application, the agent automatically reasons about this need, hosts the app, and generates a preview link for you to inspect the live result:

RPG game demo generated by coding agent

You can continue interacting with your agent until you are finished. When you exit the program, the sandbox will be deleted automatically.

2. Project Setup

Clone the Repository

First, clone the daytona repository and navigate to the example directory:

Terminal window
git clone https://github.com/daytonaio/daytona.git
cd daytona/guides/typescript/claude-agent-sdk

Configure Environment

Get your API keys:

Copy .env.example to .env and add your keys:

Terminal window
DAYTONA_API_KEY=your_daytona_key
SANDBOX_ANTHROPIC_API_KEY=your_anthropic_key

Local Usage

Install dependencies:

Terminal window
npm install

Run the agent:

Terminal window
npm run start

The agent will start and wait for your prompt.

3. Example Usage

Example chat session:

Click to view example chat session
$ npm run start
Creating sandbox...
Installing Agent SDK...
Initializing Agent SDK...
Press Ctrl+C at any time to exit.
User: Create a fun pet store landing page in Svelte
Thinking...
I'll create a fun pet store landing page in Svelte for you! Let me set up the project and build an engaging landing page with animations and a colorful design.
๐Ÿ”จ Bash
๐Ÿ”จ Bash
Now let me create a fun and engaging pet store landing page with animations, colorful sections, and interactive elements:
๐Ÿ”จ Read
> ๐Ÿ”จ Write
Now let me create the component files:
๐Ÿ”จ Write
๐Ÿ”จ Write
๐Ÿ”จ Write
๐Ÿ”จ Write
๐Ÿ”จ Write
Now let's update the main CSS file and start the development server:
๐Ÿ”จ Read
๐Ÿ”จ Write
Now let's start the development server:
๐Ÿ”จ Bash
๐Ÿ”จ TaskOutput
Perfect! ๐ŸŽ‰ I've created a fun and colorful pet store landing page in Svelte! Here's what I built:
## Features:
โœจ Hero Section
- Beautiful gradient background with animated floating cards
- Eye-catching navigation bar
- Call-to-action buttons with hover effects
- Bouncing emoji animations
๐ŸŽฏ Features Section
- 4 key benefits (Free Delivery, Premium Quality, Special Offers, Award Winning)
- Interactive cards with hover animations
- Icons that rotate and scale on hover
๐Ÿ›๏ธ Products Section
- 6 featured products with colorful backgrounds
- Star ratings
- Add to cart functionality
- Smooth hover animations
๐Ÿ“ง Newsletter Section
- Vibrant gradient background with floating pet emojis
- Email subscription form
- Success message animation
- Special 20% off offer
๐Ÿ‘ฃ Footer
- Comprehensive navigation links
- Social media icons
- Dark gradient design
## Live Preview:
Your pet store is now running at: https://80-b3739199-d90e-4bde-ab18-450d74c950e8.proxy.daytona.works
The page includes:
- Smooth scroll behavior
- Responsive design for mobile devices
- Fun animations throughout
- Interactive elements with hover effects
- Colorful gradients and modern styling
- Emoji-based icons for a playful feel
Click the link to see your fun pet store landing page in action! ๐Ÿพ
User:

4. Understanding the Agentโ€™s Architecture

This example consists of two main components:

  • Main Program: The main program is a Node.js script (index.ts) that runs on your local machine. It uses the Daytona SDK to create and manage a Daytona sandbox. The main program provides a command line interface for interacting with the agent inside the sandbox.
  • Sandbox Agent: The sandbox agent is a Python script (coding_agent.py) that runs inside the Daytona sandbox. It uses the Claude Agent SDK to create a customized coding agent similar to Claude Code.

Initialization

On initialization, the main program:

  1. Creates a new Daytona sandbox with your Anthropic API key included in the environment variables.
  2. Installs the Claude Agent SDK by running pip install in the sandbox with process execution.
  3. Creates a new code interpreter context.
  4. Uploads the coding agent script to the sandbox with file uploading.
  5. Initializes the Claude Agent SDK by running import coding_agent in the code interpreter context.
  6. Waits for user input and sends prompts to the agent in the code interpreter context as shown below.

Main Program Code

Once the agent is running, the program creates a readline interface to read user input and sends it to the agent. Each user request is passed to the agent by running a Python command in the code interpreter context:

const result = await sandbox.codeInterpreter.runCode(
`coding_agent.run_query_sync(os.environ.get('PROMPT', ''))`,
{
context: ctx,
envs: { PROMPT: prompt },
onStdout,
onStderr,
}
);

The onStdout and onStderr callbacks are used to pass the agentโ€™s output back to the main program. After the agent finishes responding to the prompt, the main program waits for the next user input.

Sandbox Agent Code

The sandbox agent uses the Claude Agent SDK to create a customized coding agent based on Claude Code. The agent is initialized with a system prompt that includes the workspace directory and an example of the preview URL format:

system_prompt = """
You are running in a Daytona sandbox.
Use the /home/daytona directory instead of /workspace for file operations.
Your public preview URL for port 80 is: {}.
""".format(preview_url)

It also specifies the tools and permission mode of the agent:

client = ClaudeSDKClient(
options=ClaudeAgentOptions(
allowed_tools=["Read", "Edit", "Glob", "Grep", "Bash"],
permission_mode="acceptEdits",
system_prompt=system_prompt
)
)

The code to run queries and receive responses follows the examples in Anthropicโ€™s Claude Agent Python SDK documentation.

Clean up

When you exit the main program, the Daytona sandbox and all files are automatically deleted.

Key advantages:

  • Secure, isolated execution in Daytona sandboxes
  • Communicate with the agent directly in your terminal
  • Automatic dev server detection and live preview links
  • Multi-language and full-stack support
  • Simple setup and automatic cleanup