How to Use userContext Sent via the Public API in Generative AI Prompts
When calling the Advanced Search API, you can pass user-specific information through the customData.userContext field. This context is available in the Query Rephrase feature, allowing the LLM to tailor how it interprets and rephrases user queries based on who is asking.
userContext is available in the Query Rephrase prompt only. It is not available in the Answer Generation prompt.
User context can be passed in the API as a customData field. When userContext is included in the API request, Search AI makes it available in the prompt template as the {{customUserContext}} variable The entire object is passed as-is to the LLM, all keys and values in the object are visible to the model, allowing it to reason over them when rephrasing the query.For example, if you send:
In the prompt configuration for the Query Rephrase Agent, use {{customUserContext}} to reference the user context and instruct the LLM to apply rephrasing logic based on its contents.Example prompt configuration:
"messages": [ { "role": "system", "content": "You are an intelligent assistant that helps users rephrase their queries only when necessary based on userContext. If the userContext has userType as active, rephrase the query to focus on currently active employee benefits and entitlements." }, { "role": "user", "content": "Using the following userContext, rephrase the query if needed: {{query}}.\nContext: {{customUserContext}}." }]
In this example, the LLM receives the full userContext object and uses the userType field to decide how to rephrase the query. For a user with userType: active, the query “what are my benefits?” might be rephrased to “what benefits and entitlements are available to currently active employees?” before retrieval.