E2B is an open-source runtime for executing AI-generated code in secure cloud sandboxes. Made for agentic & AI use cases.

E2B is a perfect fit to build Coding Agents that can write code, fix bugs, and more.

Setup

1

Install AgentKit and E2B

Within an existing project, Install AgentKit and E2B from npm:

npm install @inngest/agent-kit @e2b/code-interpreter

2

Setup your Coding Agent

Create a Agent and its associated Network:

import {
  createAgent,
  createNetwork,
  anthropic
} from "@inngest/agent-kit";

const agent = createAgent({
  name: "Coding Agent",
  description: "An expert coding agent",
  system: `You are a coding agent help the user to achieve the described task.

  Once the task completed, you should return the following information:
  <task_summary>
  </task_summary>

  Think step-by-step before you start the task.
  `,
  model: anthropic({
    model: "claude-3-5-sonnet-latest",
    max_tokens: 4096,
  }),
});

const network = createNetwork({
  name: "Coding Network",
  agents: [agent],
  defaultModel: anthropic({
    model: "claude-3-5-sonnet-20240620",
    maxTokens: 1000,
  })
});

3

Create the E2B Tools

To operate, our Coding Agent will need to create files and run commands.

Below is an example of how to create the createOrUpdateFiles and terminal E2B tools:

import {
  createAgent,
  createNetwork,
  anthropic,
  createTool
} from "@inngest/agent-kit";

const agent = createAgent({
  name: "Coding Agent",
  description: "An expert coding agent",
  system: `You are a coding agent help the user to achieve the described task.

  Once the task completed, you should return the following information:
  <task_summary>
  </task_summary>

  Think step-by-step before you start the task.
  `,
  model: anthropic({
    model: "claude-3-5-sonnet-latest",
    max_tokens: 4096,
  }),
  tools: [
    // terminal use
    createTool({
      name: "terminal",
      description: "Use the terminal to run commands",
      parameters: z.object({
        command: z.string(),
      }),
      handler: async ({ command }, { network }) => {
        const buffers = { stdout: "", stderr: "" };

        try {
          const sandbox = await getSandbox(network);
          const result = await sandbox.commands.run(command, {
            onStdout: (data: string) => {
              buffers.stdout += data;
            },
            onStderr: (data: string) => {
              buffers.stderr += data;
            },
          });
          return result.stdout;
        } catch (e) {
          console.error(
            `Command failed: ${e} \nstdout: ${buffers.stdout}\nstderr: ${buffers.stderr}`
          );
          return `Command failed: ${e} \nstdout: ${buffers.stdout}\nstderr: ${buffers.stderr}`;
        }
      },
    }),
    // create or update file
    createTool({
      name: "createOrUpdateFiles",
      description: "Create or update files in the sandbox",
      parameters: z.object({
        files: z.array(
          z.object({
            path: z.string(),
            content: z.string(),
          })
        ),
      }),
      handler: async ({ files }, { network }) => {
        try {
          const sandbox = await getSandbox(network);
          for (const file of files) {
            await sandbox.files.write(file.path, file.content);
          }
          return `Files created or updated: ${files
            .map((f) => f.path)
            .join(", ")}`;
        } catch (e) {
          return "Error: " + e;
        }
      },
    }),
  ]
});

const network = createNetwork({
  name: "Coding Network",
  agents: [agent],
  defaultModel: anthropic({
    model: "claude-3-5-sonnet-20240620",
    maxTokens: 1000,
  })
});

You will find the complete example in the E2B Coding Agent example.

Designing useful tools

As covered in Anthropic’s “Tips for Building AI Agents”, the best Agents Tools are the ones that you will need to accomplish the task by yourself.

Do not map tools directly to the underlying API, but rather design tools that are useful for the Agent to accomplish the task.

Examples