Skip to main content
The entire code generation pipeline runs inside a single Inngest function. Inngest handles retries, step-level durability, concurrency control, and real-time event publishing — so long-running AI agent runs are resilient to transient failures without any custom infrastructure.

Client setup

The Inngest client is created once and re-used across the application. It includes the realtimeMiddleware to enable the publish helper inside function handlers.
inngest/client.ts
import { Inngest } from "inngest";
import { realtimeMiddleware } from "@inngest/realtime/middleware";

export const inngest = new Inngest({
  id: "code-agent",
  middleware: [realtimeMiddleware()],
});

Function: code-agent

The function is triggered by the code-agent/codeAgent.run event and processes one project at a time.
inngest/functions.ts
export const codeAgentFunction = inngest.createFunction(
  {
    id: "code-agent",
    concurrency: {
      limit: 1,
      key: "event.data.projectId",
    },
  },
  { event: "code-agent/codeAgent.run" },
  async ({ event, step, publish }) => { ... },
);

Event payload

{
  userId: string;
  projectId: string;
  message: string;
  imageUrl?: string; // optional design screenshot URL
}

Concurrency

The concurrency configuration ensures that at most one run of code-agent executes for a given projectId at any time. Additional triggers for the same project queue behind the active run.
Triggering multiple concurrent builds on the same project will not cause conflicts — Inngest serialises them automatically — but users may experience a delay while waiting for the previous run to finish.

Function steps

Each step.run call is a durable checkpoint. If the function is interrupted, Inngest replays from the last completed step.
1

get-or-create-sandbox

Looks up the project’s stored sandboxId. Reconnects to the existing E2B sandbox or creates a new forgeai-v1 sandbox and persists the ID to the database.
2

get-prev-messages

Loads the 6 most recent messages for the project from the database and converts them to @inngest/agent-kit Message objects. These seed the agent’s conversation history.
3

get-prev-code-files

Fetches the file map from the most recent code fragment for this project. This gives the agent the current state of the codebase without reading every file from the sandbox.
4

build-agent-input

If an imageUrl is present in the event payload, calls extractDesignSpecFromImage (GPT-4o) to produce a structured design spec and prepends it to the user’s message. Otherwise passes the message through unchanged.
5

run network

Runs the @inngest/agent-kit network. The coding agent iterates up to 20 times, calling sandbox tools until it emits a <task_summary> in its response.
6

get-sandbox-url

Retrieves the public HTTPS URL for port 3000 on the E2B sandbox via sandbox.getHost(3000).
7

generate-metadata

Runs the naming agent (GPT-5.2) to generate a short project title and saves it to the project record in the database.
8

save-to-db

Creates an ASSISTANT message and an associated CodeFragment record containing the sandbox URL, sandbox ID, and the full file map. If no files were produced, saves an ERROR message instead.

Real-time status updates

The function publishes status strings to a per-project Inngest Realtime channel throughout execution. The UI subscribes to this channel to show live progress.
inngest/functions.ts
import { channel, topic } from "@inngest/realtime";

export const projectChannel = channel(
  (projectId: string) => `Project:${projectId}`,
).addTopic(topic("projectInfo").type<string>());

// Inside tool handlers and steps:
await publish(
  await projectChannel(event.data.projectId).projectInfo("Installing packages..."),
);
Status strings published during a typical run:
StepStatus string
terminal tool"Installing packages..."
createOrUpdateFiles tool"Generating project files..."
readFiles tool"Reading project files..."
unsplashImage tool"Downloading images..."
get-sandbox-url"Generating sandbox url..."
generate-metadata"Generating project name..."
save-to-db"Saving to db..."
Function complete"Demo is ready"

AI models

The agent network and models that run inside the Inngest function.

E2B sandbox

The isolated execution environment the function provisions and uses.

Build docs developers (and LLMs) love