Skip to main content
AMP is Sourcegraph’s coding agent with multi-model architecture and built-in code intelligence. E2B provides a pre-built amp template with AMP already installed.

CLI

Spin up a sandbox with the E2B CLI.
e2b sbx create amp
Once inside the sandbox, start AMP.
amp

Run Headless

Use -x for non-interactive mode and --dangerously-allow-all to auto-approve all tool calls (safe inside E2B sandboxes). AMP uses its own API key from ampcode.com/settings.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('amp', {
  envs: { AMP_API_KEY: process.env.AMP_API_KEY },
})

const result = await sandbox.commands.run(
  `amp --dangerously-allow-all -x "Create a hello world HTTP server in Go"`
)

console.log(result.stdout)
await sandbox.kill()

Example: work on a cloned repository

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('amp', {
  envs: { AMP_API_KEY: process.env.AMP_API_KEY },
  timeoutMs: 600_000,
})

await sandbox.git.clone('https://github.com/your-org/your-repo.git', {
  path: '/home/user/repo',
  username: 'x-access-token',
  password: process.env.GITHUB_TOKEN,
  depth: 1,
})

const result = await sandbox.commands.run(
  `cd /home/user/repo && amp --dangerously-allow-all -x "Add error handling to all API endpoints"`,
  { onStdout: (data) => process.stdout.write(data) }
)

const diff = await sandbox.commands.run('cd /home/user/repo && git diff')
console.log(diff.stdout)

await sandbox.kill()

Streaming JSON

Use --stream-json to get a real-time JSONL event stream with rich metadata — including tool calls, token usage, thinking blocks, and permission decisions.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('amp', {
  envs: { AMP_API_KEY: process.env.AMP_API_KEY },
})

const result = await sandbox.commands.run(
  `cd /home/user/repo && amp --dangerously-allow-all --stream-json -x "Find and fix all TODO comments"`,
  {
    onStdout: (data) => {
      for (const line of data.split('\n').filter(Boolean)) {
        const event = JSON.parse(line)
        if (event.type === 'assistant') {
          console.log(`[assistant] tokens: ${event.message.usage?.output_tokens}`)
        } else if (event.type === 'result') {
          console.log(`[done] ${event.message.subtype} in ${event.message.duration_ms}ms`)
        }
      }
    },
  }
)

await sandbox.kill()

Thread Management

AMP persists conversations as threads that can be resumed or continued with follow-up tasks.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('amp', {
  envs: { AMP_API_KEY: process.env.AMP_API_KEY },
  timeoutMs: 600_000,
})

// Start a new thread
const initial = await sandbox.commands.run(
  `cd /home/user/repo && amp --dangerously-allow-all -x "Analyze the codebase and create a refactoring plan"`,
  { onStdout: (data) => process.stdout.write(data) }
)

// List threads and get the most recent thread ID
const threads = await sandbox.commands.run('amp threads list --json')
const threadId = JSON.parse(threads.stdout)[0].id

// Continue the thread with a follow-up task
const followUp = await sandbox.commands.run(
  `cd /home/user/repo && amp threads continue ${threadId} --dangerously-allow-all -x "Now implement step 1 of the plan"`,
  { onStdout: (data) => process.stdout.write(data) }
)

const diff = await sandbox.commands.run('cd /home/user/repo && git diff')
console.log(diff.stdout)

await sandbox.kill()

Build a Custom Template

If you need to customize the environment (e.g. pre-install dependencies, add config files), build your own template on top of the pre-built amp template.
// template.ts
import { Template } from 'e2b'

export const template = Template()
  .fromTemplate('amp')
// build.ts
import { Template, defaultBuildLogger } from 'e2b'
import { template as ampTemplate } from './template'

await Template.build(ampTemplate, 'my-amp', {
  cpuCount: 2,
  memoryMB: 2048,
  onBuildLogs: defaultBuildLogger(),
})
Run the build script to create the template.
npx tsx build.ts