# Claude Code
<subtitle>Provides Claude Code Agent in the sandbox. </subtitle>

> For a complete guide to running Claude Code in UCloud Sandbox—including working with code repositories, streaming output, and more—see [Code Agent Best Practices](/docs/agent-sandbox/best-practices/codeagent.md).

## Template definition

```python
# template.py
from ucloud_sandbox import Template

template = (
    Template()
    .from_node_image("24")
    .apt_install(["curl", "git", "ripgrep"])
    # Claude Code will be available globally as "claude"
    .npm_install("@anthropic-ai/claude-code@latest", g=True)
)
```

## Build template

```python
# build.py
from ucloud_sandbox import Template, default_build_logger
from .template import template as claudeCodeTemplate

Template.build(claudeCodeTemplate, 'claude-code',
    cpu_count=1,
    memory_mb=1024,
    on_build_logs=default_build_logger(),
)
```

## Create a sandbox from a template

```python
# sandbox.py
from ucloud_sandbox import Sandbox

sbx = Sandbox.create(
    'claude-code',
    envs={
        'ANTHROPIC_API_KEY': '<your api key>',
    },
)
print("Sandbox created", sbx.sandbox_id)

# Print Claude Code help information
# result = sbx.commands.run('claude --help')
# print(result.stdout)

# Run a prompt using Claude Code
result = sbx.commands.run(
    "claude --dangerously-skip-permissions -p 'Create a hello world index.html'",
    timeout=0,
)
print(result.stdout)

sbx.kill()
```
