-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
docs(ai-agents): add chat.agent guide and refresh the AI agent guides #4524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
D-K-P
wants to merge
4
commits into
main
Choose a base branch
from
docs-ai-agents-chat-agent-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
92f1135
docs(ai-agents): add chat.agent guide and refresh the AI agent guides
D-K-P 488683b
docs(ai-agents): address review feedback on the chat agent guide
D-K-P d0a3827
docs(ai-agents): fix tool wiring and recursive return type in the guides
D-K-P 3928779
Merge branch 'main' into docs-ai-agents-chat-agent-guide
D-K-P File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| --- | ||
| title: "Build a chat agent" | ||
| sidebarTitle: "Chat agent" | ||
| description: "Create a durable, multi-turn chat agent with chat.agent(), then add tools to it like any AI SDK agent." | ||
| --- | ||
|
|
||
| ## Overview | ||
|
|
||
| Build a **durable, multi-turn chat agent**. A durable session owns the conversation, streams tokens to your UI, and stays alive across many back-and-forth messages. The other guides in this section are one-shot workflows (trigger a task, run a fixed sequence of LLM calls, return a result); a chat agent instead owns the session for its whole lifetime. | ||
|
|
||
| [`chat.agent()`](/ai-chat/overview) handles the queuing, retries, resumability and streaming for you. You write the model call, Trigger.dev owns the session. For the full feature set (sessions, fast starts, compaction, sub-agents, the frontend transport), see the [AI chat docs](/ai-chat/overview). | ||
|
|
||
| ## A minimal agent | ||
|
|
||
| Define an agent with `chat.agent()`. The `run` function receives the conversation `messages` (already converted from the frontend's `UIMessage[]`) and an abort `signal`. Return a `StreamTextResult` and it's piped to the frontend automatically. | ||
|
|
||
| ```typescript trigger/chat.ts | ||
| import { chat } from "@trigger.dev/sdk/ai"; | ||
| import { anthropic } from "@ai-sdk/anthropic"; | ||
| import { streamText, stepCountIs } from "ai"; | ||
|
|
||
| export const myChat = chat.agent({ | ||
| id: "my-chat", | ||
| run: async ({ messages, signal }) => { | ||
| return streamText({ | ||
| // Spread chat.toStreamTextOptions() FIRST: it wires up prepareStep | ||
| // (compaction, steering, background injection) and telemetry. | ||
| ...chat.toStreamTextOptions(), | ||
| model: anthropic("claude-sonnet-4-5"), | ||
| messages, | ||
| abortSignal: signal, | ||
| stopWhen: stepCountIs(15), | ||
| }); | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| <Warning> | ||
| Always spread `chat.toStreamTextOptions()` into your `streamText` call, and spread it first. It | ||
| wires up the `prepareStep` callback that drives compaction, mid-turn steering and background | ||
| injection. Those features silently no-op if the spread is missing. | ||
| </Warning> | ||
|
|
||
| ## Add tools | ||
|
|
||
| A chat agent uses tools exactly like any other AI SDK agent. Declare them on the config so their results survive across turns, then pass the `tools` you receive in `run` straight to `streamText`: | ||
|
|
||
| ```typescript trigger/chat.ts | ||
| import { chat } from "@trigger.dev/sdk/ai"; | ||
| import { anthropic } from "@ai-sdk/anthropic"; | ||
| import { streamText, stepCountIs, tool } from "ai"; | ||
| import { z } from "zod"; | ||
|
|
||
| const getCurrentTime = tool({ | ||
| description: "Get the current server time as an ISO string.", | ||
| inputSchema: z.object({}), | ||
| execute: async () => ({ now: new Date().toISOString() }), | ||
| }); | ||
|
|
||
| export const myChat = chat.agent({ | ||
| id: "my-chat", | ||
| // Declared here so tool results survive history re-conversion across turns. | ||
| tools: { getCurrentTime }, | ||
| run: async ({ messages, tools, signal }) => { | ||
| return streamText({ | ||
| // Pass tools INTO toStreamTextOptions (not separately to streamText): | ||
| // this is what detects tool calls needing HITL approval and merges any | ||
| // auto-injected skill tools. It sets streamText's `tools` for you. | ||
| ...chat.toStreamTextOptions({ tools }), | ||
| model: anthropic("claude-sonnet-4-5"), | ||
| messages, | ||
| stopWhen: stepCountIs(15), | ||
| abortSignal: signal, | ||
| }); | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Swap `getCurrentTime` for whatever your agent needs to do: query a database, call an API, or trigger another Trigger.dev task. See [Tools](/ai-chat/tools) for how tool results are persisted and replayed across turns. | ||
|
|
||
| ## Wire up the frontend | ||
|
|
||
| The browser talks to Trigger.dev directly through the [chat transport](/ai-chat/frontend), so there's no API route to maintain. Expose two server actions (one to start the session, one to mint a session-scoped token) and pass them to `useTriggerChatTransport`, then hand the transport to the AI SDK's `useChat`: | ||
|
|
||
| ```typescript app/actions.ts | ||
| "use server"; | ||
|
|
||
| import { auth } from "@trigger.dev/sdk"; | ||
| import { chat } from "@trigger.dev/sdk/ai"; | ||
|
|
||
| export const startChatSession = chat.createStartSessionAction("my-chat"); | ||
|
|
||
| export async function mintChatAccessToken(chatId: string) { | ||
| // Authorize the caller for this chatId before minting: confirm the logged-in | ||
| // user owns this session (e.g. look it up in your database). Otherwise anyone | ||
| // who learns a session ID could mint read/write access to it. | ||
| return auth.createPublicToken({ | ||
| scopes: { read: { sessions: chatId }, write: { sessions: chatId } }, | ||
| expirationTime: "1h", | ||
| }); | ||
|
D-K-P marked this conversation as resolved.
|
||
| } | ||
| ``` | ||
|
|
||
| See the [Quick Start](/ai-chat/quick-start) for the complete frontend component. | ||
|
|
||
| ## A full example | ||
|
|
||
| For a complete, real-world chat agent, see the ClickHouse chat agent example. It builds on everything above with generative UI, a versioned system prompt, and real tools against a live database. | ||
|
|
||
| <CardGroup cols={2}> | ||
| <Card title="ClickHouse chat agent" icon="chart-column" href="/guides/example-projects/clickhouse-chat-agent"> | ||
| A full example project: a chat agent that answers questions about your data with charts, tables | ||
| and maps. | ||
| </Card> | ||
| <Card title="AI chat overview" icon="message-bot" href="/ai-chat/overview"> | ||
| How chat agents, sessions and the turn loop work. | ||
| </Card> | ||
| <Card title="Tools" icon="wrench" href="/ai-chat/tools"> | ||
| Declaring tools on your agent and how they persist across turns. | ||
| </Card> | ||
| <Card title="Fast starts" icon="bolt" href="/ai-chat/fast-starts"> | ||
| Cut first-turn latency with preload and head start. | ||
| </Card> | ||
| </CardGroup> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.