Build a Coding Agent Using Claude Agent SDK and Daytona
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 174sCreating 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 programmersThinking...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๐จ WriteNow let me start a simple HTTP server to host the game:๐จ BashPerfect! 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 Linux2. Grace Hopper - COBOL pioneer and Admiral3. Alan Turing - Father of computer science4. Ada Lovelace - First computer programmer5. 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:
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:
git clone https://github.com/daytonaio/daytona.gitcd daytona/guides/typescript/claude-agent-sdkConfigure Environment
Get your API keys:
- Daytona API key: Daytona Dashboard
- Anthropic API key: Anthropic Console
Copy .env.example to .env and add your keys:
DAYTONA_API_KEY=your_daytona_keySANDBOX_ANTHROPIC_API_KEY=your_anthropic_keyLocal Usage
Install dependencies:
npm installRun the agent:
npm run startThe agent will start and wait for your prompt.
3. Example Usage
Example chat session:
Click to view example chat session
$ npm run startCreating sandbox...Installing Agent SDK...Initializing Agent SDK...Press Ctrl+C at any time to exit.User: Create a fun pet store landing page in SvelteThinking...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๐จ BashNow let me create a fun and engaging pet store landing page with animations, colorful sections, and interactive elements:๐จ Read> ๐จ WriteNow let me create the component files:๐จ Write๐จ Write๐จ Write๐จ Write๐จ WriteNow let's update the main CSS file and start the development server:๐จ Read๐จ WriteNow let's start the development server:๐จ Bash๐จ TaskOutputPerfect! ๐ 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:
- Creates a new Daytona sandbox with your Anthropic API key included in the environment variables.
- Installs the Claude Agent SDK by running
pip installin the sandbox with process execution. - Creates a new code interpreter context.
- Uploads the coding agent script to the sandbox with file uploading.
- Initializes the Claude Agent SDK by running
import coding_agentin the code interpreter context. - 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