Skip to content

Commit e4c9059

Browse files
committed
docs(ai-chat): chat.headStart guide + reference + changelog
- New head-start.mdx: walkthrough of when/why/how, with framework CodeGroups for Web Fetch (Next.js, Hono, SvelteKit, Remix, TanStack Start, Astro, Nitro/Nuxt, Elysia), Edge/standalone (Workers, Bun, Deno), and Node-only (Express, Fastify, Koa, raw node:http via chat.toNodeListener). Mermaid diagram covering pure-text and tool-call paths. Bundle-isolation rationale and limitations. - features.mdx: link from the headline list. - reference.mdx: chat.headStart, chat.openSession, chat.toNodeListener signatures + HandoverChatHelper / HandoverSession types. - changelog.mdx: head-start entry under Upcoming. - docs.json: register the new page in the AI Chat sidebar.
1 parent 4df153b commit e4c9059

5 files changed

Lines changed: 558 additions & 0 deletions

File tree

docs/ai-chat/changelog.mdx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,74 @@ sidebarTitle: "Changelog"
44
description: "Pre-release updates for AI chat agents."
55
---
66

7+
<Update label="May 3, 2026" tags={["SDK"]}>
8+
9+
## `chat.headStart` — fast first-turn for chat.agent
10+
11+
A new opt-in flow that cuts first-turn TTFC roughly in half by running step 1's LLM call in your warm process while the chat.agent run boots in parallel. On the LLM's `tool-calls` boundary, ownership of the durable stream hands over to the agent for tool execution and step 2+. Pure-text first turns finish on the customer side with no LLM call from the trigger run at all.
12+
13+
Measured on `claude-sonnet-4-6` (same model both sides): TTFT 2801ms → 1218ms (−57%), total turn 4180ms → 2345ms (−44%). With Head Start, first-text time is essentially the LLM TTFB floor.
14+
15+
### Setup
16+
17+
```ts app/api/chat/route.ts
18+
import { chat } from "@trigger.dev/sdk/chat-server";
19+
import { streamText } from "ai";
20+
import { anthropic } from "@ai-sdk/anthropic";
21+
import { headStartTools } from "@/lib/chat-tools/schemas";
22+
23+
export const POST = chat.headStart({
24+
agentId: "my-chat",
25+
run: async ({ chat: helper }) =>
26+
streamText({
27+
...helper.toStreamTextOptions({ tools: headStartTools }),
28+
model: anthropic("claude-sonnet-4-6"),
29+
system: "You are a helpful assistant.",
30+
}),
31+
});
32+
```
33+
34+
```tsx components/chat.tsx
35+
const transport = useTriggerChatTransport({
36+
task: "my-chat",
37+
accessToken: ({ chatId }) => mintChatAccessToken(chatId),
38+
startSession: ({ chatId, taskId, clientData }) =>
39+
startChatSession({ chatId, taskId, clientData }),
40+
headStart: "/api/chat",
41+
});
42+
```
43+
44+
### Bundle isolation
45+
46+
Tool schemas (`description` + `inputSchema`) live in their own module that imports only `ai` and `zod`. The agent task imports those schemas and adds heavy `execute` fns. The route handler imports schemas only — keeping the warm-process bundle light is what makes the win possible. Runtime "strip executes" helpers don't solve this — bundlers resolve imports at build time. See [Head Start → Setup](/ai-chat/head-start#setup) for the full split.
47+
48+
### Compared to Preload
49+
50+
Preload eagerly triggers the run on page load (good when you're confident the user *will* send a message — trades idle compute for fast TTFC). Head Start gates the run on a real first message — no idle compute, customer's process runs step 1 directly. Pick one per chat.
51+
52+
### Works on every runtime
53+
54+
`chat.headStart` returns a standard Web Fetch handler — `(req: Request) => Promise<Response>` — so it slots into Next.js App Router, Hono, SvelteKit, Remix / React Router v7, TanStack Start, Astro, Nitro/Nuxt, Elysia, Cloudflare Workers, Bun, Deno, and any other runtime that speaks Web Fetch. Verified runtimes: Node 18+, Bun, Deno, Workers, Vercel (Node and Edge), Netlify (Functions and Edge).
55+
56+
For Node-only frameworks (Express, Fastify, Koa, raw `node:http`), the SDK ships `chat.toNodeListener(handler)` — converts any Web Fetch handler into a Node `(req, res)` listener with proper streaming, header translation, and client-disconnect propagation.
57+
58+
```ts
59+
import express from "express";
60+
import { chat } from "@trigger.dev/sdk/chat-server";
61+
62+
const handler = chat.headStart({ agentId: "my-chat", run: ... });
63+
64+
const app = express();
65+
app.post("/api/chat", chat.toNodeListener(handler));
66+
```
67+
68+
## Docs
69+
70+
- New [Head Start guide](/ai-chat/head-start) — bundle isolation, schema/execute split, route handler setup, transport option, lifecycle, limitations.
71+
- [Reference](/ai-chat/reference#triggerchattransport-options)`headStart` transport option.
72+
73+
</Update>
74+
775
<Update label="May 2, 2026" description="0.0.0-chat-prerelease-20260502065709" tags={["SDK"]}>
876

977
## Resilient SSE reconnection

docs/ai-chat/features.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ export const mySubtask = schemaTask({
423423

424424
Preload eagerly triggers a run for a chat before the first message is sent. This allows initialization (DB setup, context loading) to happen while the user is still typing, reducing first-response latency.
425425

426+
<Tip>
427+
See also [Head Start](/ai-chat/head-start) — solves the same first-turn TTFC problem from the other direction. Preload trades idle compute for fast TTFC (good when the user *will* send a message). Head Start runs step 1's LLM call in your warm process while the agent run boots in parallel — no idle compute, gates the run on a real first message.
428+
</Tip>
429+
430+
426431
### Frontend
427432

428433
Call `transport.preload(chatId)` to start a run early:

0 commit comments

Comments
 (0)