Skip to content

Using AgentKit with E2B

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.

  1. Install AgentKit and E2B

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

    Terminal window
    npm install @inngest/agent-kit inngest @e2b/code-interpreter
    Don’t have an existing project?

    To create a new project, create a new directory then initialize using your package manager:

    Terminal window
    mkdir my-agent-kit-project && npm init
  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.