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.
-
Install AgentKit and E2B
Within an existing project, Install AgentKit and E2B from npm:
Terminal window npm install @inngest/agent-kit inngest @e2b/code-interpreterTerminal window pnpm install @inngest/agent-kit inngest @e2b/code-interpreterTerminal window yarn add @inngest/agent-kit inngest @e2b/code-interpreterDon’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 initTerminal window mkdir my-agent-kit-project && pnpm initTerminal window mkdir my-agent-kit-project && yarn init -
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,})}); -
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
createOrUpdateFilesandterminalE2B 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 usecreateTool({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 filecreateTool({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.
Examples
Section titled “Examples” Replicate Cursor's Agent mode This examples shows how to use E2B sandboxes to build a coding agent that can write code and run commands to generate complete project, complete refactoring and fix bugs.
AI-powered CSV contacts importer Let's reinvent the CSV upload UX with an AgentKit network leveraging E2B sandboxes.