promptMessageId is provided, the context will include that message, as
well as any other messages on that same order. More details on order are in
messages.mdx, but in practice this means that
if you pass the ID of the user-submitted message as the promptMessageId and
there had already been some assistant and/or tool responses, those will be
included in the context, allowing the LLM to continue the conversation.
You can also use RAG to add extra context to your prompt.
Customizing the context
You can customize the context provided to the agent when generating messages with customcontextOptions. These can be set as defaults on the Agent, or
provided at the call-site for generateText or others.
Full context control
To have full control over which messages are passed to the LLM, you can either:- Provide a
contextHandlerto filter, modify, or enrich the context messages. - Provide all messages manually via the
messagesargument and specifycontextOptionsto use no recent or search messages. See below for how to fetch context messages manually.
Providing a contextHandler
The Agent will combine messages from search, recent, input messages, and all messages on the sameorder as the promptMessageId if that is provided.
You can customize how they are combined, as well as add or remove messages by
providing a contextHandler which returns the ModelMessage[] which will be
passed to the LLM.
You can specify a contextHandler in the Agent constructor, or at the call-site
for a single generation, which overrides any Agent default.
- Filter out messages you don’t want to include.
- Add memories or other context.
- Add sample messages to guide the LLM on how it should respond.
- Inject extra context based on the user or thread.
- Copy in messages from other threads.
- Summarize messages.
Fetch context manually
If you want to get context messages for a given prompt, without calling the LLM, you can usefetchContextWithPrompt. This is used internally to get the context
messages passed to the AI SDK generateText, streamText, etc.
As with normal generation, you can provide a prompt or messages, and/or a
promptMessageId to fetch the context messages using a given pre-saved message
as the prompt.
This will return recent and search messages combined with the input messages.
Search for messages
This is what the agent does automatically, but it can be useful to do manually, e.g. to find custom context to include. For text and vector search, you can provide atargetMessageId and/or
searchText. It will embed the text for vector search. If searchText is not
provided, it will use the target message’s text.
If targetMessageId is provided, it will only fetch search messages previous to
that message, and recent messages up to and including that message’s “order”.
This enables re-generating a response for an earlier message.
Searching other threads
If you setsearchOtherThreads to true, the agent will search across all
threads belonging to the provided userId. This can be useful to have multiple
conversations that the Agent can reference.
The search will use a hybrid of text and vector search.
Passing in messages as context
You can pass in messages as context to the Agent’s LLM, for instance to implement Retrieval-Augmented Generation. The final messages sent to the LLM will be:- The system prompt, if one is provided or the agent has
instructions - The messages found via contextOptions
- The
messagesargument passed intogenerateTextor other function calls. - If a
promptargument was provided, a final{ role: "user", content: prompt }message.
Manage embeddings manually
ThetextEmbeddingModel argument to the Agent constructor allows you to specify
a text embedding model to use for vector search.
If you set this, the agent will automatically generate embeddings for messages
and use them for vector search.
When you change models or decide to start or stop using embeddings for vector
search, you can manage the embeddings manually.
Generate embeddings for a set of messages. Optionally pass config with a usage
handler, which can be a globally shared Config.