Skip to main content
The AI Agents and Prompts surface ships as part of the v4.5 release candidate. Install with @trigger.dev/sdk@rc (or pin 4.5.0-rc.0 or later) to use these features — they aren’t yet on the latest stable, and APIs may still change before the 4.5.0 GA. See supported AI SDK versions and the AI chat changelog for details.
The first turn of a brand-new conversation pays for the chat.agent run’s cold start: dequeue, process boot, onPreload / onChatStart hooks, and only then the LLM call. Two features address this from different angles.

Picking an approach

Pick one, not both. Running both for the same chat is wasted work — Head Start gates on a real first message, so adding Preload on top eats the idle-compute cost Head Start was avoiding. Use Preload when the chat surface is browser-only, when you don’t have a warm Node/Bun/Edge process serving the page, or when you can confidently predict the user will send a message (the run never goes idle). Use Head Start when the chat lives behind a warm server (Next.js App Router, Hono, SvelteKit, Workers, etc.) and you want first-turn TTFC down at the LLM TTFB floor without any speculative run.

Preload

Preload eagerly triggers a run for a chat before the first message is sent. Initialization (DB setup, context loading) happens while the user is still typing, reducing first-response latency.

Frontend

Call transport.preload(chatId) to start a run early:
Preload is a no-op if a session already exists for this chatId. Your accessToken callback receives { chatId } and is invoked the same way on preload as on any other refresh — no special branching by purpose. See TriggerChatTransport options.

Backend

The onPreload hook fires immediately. The run then waits for the first message. When the user sends a message, onChatStart fires with preloaded: true so you can skip work that already ran:
With chat.createSession() or raw tasks, check payload.trigger === "preload" and wait for the first message:

Head Start

Head Start runs step 1’s LLM call in your warm server process while the chat.agent run boots in parallel. The user sees one continuous turn: text first from your server, then a clean handover to the agent for tool execution and any further steps. chat.headStart returns a standard Web Fetch API handler — (req: Request) => Promise<Response> — so it slots into any runtime that speaks Web Fetch. Verified runtimes: Node 18+, Bun, Deno, Cloudflare Workers, Vercel (Node and Edge), Netlify (Functions and Edge). The handler uses only fetch and Web ReadableStream / TransformStream (no node:* imports), and the S2 streaming dependency picks the right transport for each runtime automatically (HTTP/2 on Node/Deno, HTTP/1.1 on Bun/Workers/browsers). Compatible frameworks (native Web Fetch): Next.js App Router, Hono, SvelteKit, Remix, React Router v7, TanStack Start, Astro, Nitro/Nuxt, Elysia. Mount the handler directly. Node-only frameworks (Express, Fastify, Koa): the handler still works, but the framework gives you a Node IncomingMessage instead of a Web Request. Use a small adapter — examples in Mounting in your framework below. When the first turn is pure text (no tool calls), the agent run boots and exits without ever calling an LLM. You only pay for what the conversation actually needed.

Measured TTFC

3 runs each, prompt "say hi in five words", same model both sides (Anthropic Claude Sonnet 4): With Head Start, time-to-first-text is essentially the LLM TTFB floor (50ms spread). Without it, agent boot + hooks stack before the LLM call, adding 750ms of variance.

How it works

1

Browser POSTs the first message to your route handler

The transport sees headStart: "/api/chat" is set and there’s no session yet for this chat. It POSTs the wire payload (messages, chatId, metadata) to your route handler.
2

Your handler creates the session and triggers the agent run

A single apiClient.createSession round-trip both creates the chat session and triggers an agent run with trigger: "handover-prepare". The agent run boots into a wait state on session.in.
3

Your handler runs streamText step 1

streamText runs in your warm process with stopWhen: stepCountIs(1). The output is streamed to the browser as SSE while the agent run boots in parallel. Boot time (~488ms) overlaps with LLM TTFB (~389ms), fully hidden.
4

Mid-turn handover

On step 1’s tool-calls finish, your handler signals the agent and the SDK splices the agent’s step-2+ stream into the same SSE response. On pure-text finish, your handler signals handover-skip and the agent run exits clean — no LLM call from the trigger side.
5

Subsequent turns bypass the route handler

After turn 1, the transport hydrates the session PAT from response headers and writes turn 2 onward directly to session.in. Same direct-trigger path as a regular chat.agent setup.

Setup

Bundle isolation is the load-bearing constraint. Head Start only saves time because your route-handler bundle stays lightweight. Anything you import in that handler — and anything those modules import transitively — lands in the bundle. If your tool catalog with heavy execute fns (E2B, Puppeteer, native bindings, the trigger SDK runtime, Turndown, image processing, node:child_process) ends up in the bundle, you’ve put cold-start back into a different process.This is an import-chain problem, not a runtime one. A “we’ll strip the executes at runtime” helper would not fix it — bundlers resolve imports at build time. The only correct shape is to keep schemas in their own module that imports ai and zod only.
1

Split your tool definitions into schemas + executes

Schemas in one module (light deps), executes in another (heavy deps). The agent task pulls in both; the route handler pulls in schemas only.
lib/chat-tools/schemas.ts
trigger/chat-tools.ts
2

Define your chat.agent (heavy executes)

The agent uses the full tool set — these are the executes that run when step 2+ needs them.
trigger/chat.ts
3

Build the head-start handler

Call chat.headStart({ agentId, run }). It returns a standard Web Fetch handler: (req: Request) => Promise<Response>. Inside the run callback you call streamText yourself and spread chat.toStreamTextOptions({ tools }) to inherit the SDK-owned wiring (messages, schema-only tools, stopWhen: stepCountIs(1), abort signal). Add your own model and system on top.
lib/chat-handler.ts
Use the same model on both sides (route handler and chat.agent) to avoid a tone or style shift between step 1 and step 2+. Your LLM provider keys stay server-side in your warm process — Trigger.dev never holds them in this design.
Mount the handler in whatever framework you use — see Mounting in your framework below.
4

Opt in on the transport

Add headStart: "/api/chat" to useTriggerChatTransport. Subsequent turns bypass this URL automatically — accessToken and (optionally) startSession still run for the direct-trigger path on turn 2 onward.
components/chat.tsx

Mounting in your framework

chat.headStart returns a Web Fetch handler — (req: Request) => Promise<Response>. Frameworks that natively pass Web Request objects mount it as-is. Node-only frameworks (Express, Fastify, Koa) need a small adapter.

Edge / standalone runtimes

Node-only frameworks

Express, Fastify, and Koa pass Node IncomingMessage / ServerResponse objects rather than Web Request / Response. The SDK ships chat.toNodeListener that wraps any Web Fetch handler as a Node (req, res) listener — body bytes are read upfront, headers translated, the response body streamed chunk-by-chunk, and client disconnect is propagated to the handler via AbortSignal.
Don’t run express.json() (or any body-parsing middleware) before the head-start route — it consumes the request body before chat.toNodeListener can read the raw bytes. Either skip the parser for this route, or scope it to other routes.

Streaming response timeouts

The handler keeps the SSE response open until the agent run signals turn-complete (or skip, on a pure-text turn). Make sure your framework / serverless function timeout accommodates that:
  • Pure-text first turns: ~LLM TTFB (1–3 s typically).
  • Tool-calling first turns: LLM step 1 + agent boot + tool execution + step 2 LLM call. Usually 5–15 s; longer for multi-step tool use.
  • Vercel: default function timeout is 10 s on Hobby, 60 s on Pro. Set export const maxDuration = N; on the route segment.
  • Cloudflare Workers: default 30 s CPU time (paid plans up to 5 min). Streaming wall time is generally not the bottleneck.
  • AWS Lambda behind API Gateway: 29 s API Gateway hard limit; Lambda Function URL allows up to 15 min.

What gets routed where

The chat.headStart API

The run callback receives:
  • messages: UIMessage[] — user messages parsed from the request body.
  • signal: AbortSignal — fires when the request closes or the SDK times out the handover.
  • chat: HeadStartChatHelper<TTools> — exposes chat.toStreamTextOptions({ tools }) and a chat.session escape hatch for power users.
chat.toStreamTextOptions({ tools }) returns options to spread into streamText. The SDK owns these keys — overriding them will break the protocol: You bring model, system, providerOptions, prepareStep, anything else streamText accepts.

The transport option

Optional. When set, the FIRST message of a brand-new chat (no existing session state) routes through this URL. Subsequent turns bypass it and use the direct-trigger path. This is not a stock useChat endpoint — it’s not the canonical request URL for every turn, just the first-turn shortcut.

Limitations

  • First turn only. Step 2+ and turn 2+ run on the trigger side. There’s no per-turn “head start every turn” mode — the win comes from amortizing agent boot across the LLM call once.
  • Single step on the warm-server side. The handler runs stopWhen: stepCountIs(1). Multi-step handover (handler does step 1 + step 2 + …) is out of scope.
  • Your server needs an LLM provider key. The first-turn LLM call runs in your warm process, so that environment needs whatever keys the model requires. The agent’s executes still run on the Trigger.dev side with whatever environment variables they need there.
  • Browser-only chat surfaces don’t apply. Without a warm server process, there’s nowhere to run step 1 ahead of the agent run. Use Preload or eat the cold-start tax.
  • Streaming-capable runtime required. Your framework / runtime has to support streaming HTTP responses (Web Fetch Response body or equivalent). Most modern hosts do — Next.js, Hono, SvelteKit, Workers, Bun, Deno, Vercel, etc. Some legacy platforms that buffer full responses won’t deliver chunks until the turn is over, which negates the TTFC benefit (correctness still holds).
  • Non-useChat chat surfaces (Slack bots, Discord bots, custom protocols) don’t fit the chat.headStart shape — the API expects the AI SDK transport’s wire payload on input. For those, trigger the chat.agent directly from your bot handler.

Reference