Prompt quick start
Learn to send requests to the LLM using Agent Actions Prompt.
Experimental feature
This article describes an experimental Sanity feature. The APIs described are subject to change and the documentation may not be completely accurate.
Prompt is a Sanity Agent Action that lets you make large language model (LLM) requests without bringing in external AI tooling. You can run prompts from anywhere you can execute code, such as Sanity Functions, custom components, webhook listeners, CI/CD pipelines, migration scripts, and more.
In this guide, you'll first use Prompt to make a request to the LLM. You'll use @sanity/client to run the Prompt (you can also make requests using the HTTP API directly).
Prerequisites:
@sanity/clientv7.8.2 or later (Prompt shipped in v7.4.0; v7.8.2 corrects the response typing) and an environment to run client requests.- API version
vXis required for any requests to the Agent Actions APIs. - AI features enabled for your organization. If an organization turns them off, every Agent Actions request fails with a
403. See Troubleshoot Agent Actions requests. - Optional: In Node.js v23.6 and above, you can run the TypeScript examples below without additional servers or build processes. Alternatively, you can use earlier versions with an experimental flag. Converting the examples to JavaScript is okay too.
- An API or personal token to make authenticated requests. Store it in the environment rather than in your code, for example as
SANITY_API_TOKEN.
Step 1: Configure the client
Import and configure @sanity/client with the projectId, dataset, API token, and an apiVersion of vX.
import { createClient } from "@sanity/client";
export const client = createClient({
projectId: 'YOUR_PROJECT_ID',
dataset: 'production',
apiVersion: 'vX',
token: process.env.SANITY_API_TOKEN
})Step 2: Send your first prompt
Prompts don't edit or create documents. They return a string, or JSON.
const response = await client.agent.action.prompt({
// write an instruction
instruction: `Give me some ideas for a blog post
about using AI with structured content.`
});
console.log(response)This prompt returns a text response that you can use. The examples on this page log the response to the console.
Step 3: Use parameters
You can further customize the instruction by passing in parameters. For example, if you want to use a document as background information for the prompt, you use the document parameter type.
const response = await client.agent.action.prompt({
// write an instruction
instruction: `Give me some ideas for a blog post
about using AI with structured content. Use the following as context for the ideas: $background`,
instructionParams: {
background: {
type: 'document',
documentId: 'YOUR_DOCUMENT_ID'
}
}
});
console.log(response)You can learn more about parameters and the available types in the creating instructions guide.
Step 4: Change the output
Prompt will return a string response by default, but you can also tell it to return JSON. To do so, you must set the format to "json" and explicitly include the word "JSON" or "json" in the instruction. It also helps to provide an example shape in the instruction.
const response = await client.agent.action.prompt({
// write an instruction
instruction: `Give me some ideas for a blog post
about using AI with structured content. Respond in JSON with the following format: { "ideas": ["idea one", "idea two"] }`,
format: 'json'
});
console.log(response)Step 5: Add variety to responses
You can tune the variance of responses by adjusting the temperature of the request.
const response = await client.agent.action.prompt({
// write an instruction
instruction: `Give me some ideas for a blog post
about using AI with structured content.`,
temperature: 0.8 // Set between 0 and 1, inclusively. Default: 0.3
});
console.log(response)Higher values result in more variety of responses, while lower values result in more predictable results when given the same instruction.
Next steps
To learn more about what you can do with Prompt, explore the other guides and resources available for Agent Actions.
If you're already using the client elsewhere in an application, you can reuse its base configuration. To adjust the token, the API version, or both, use the withConfig method to create a new client based on your existing one. For example:
// ...
const promptClient = client.withConfig({
token: process.env.SANITY_API_TOKEN,
})Agent Actions patterns
Explore common patterns across all Agent Actions
Troubleshoot Agent Actions requests
Diagnose failed Agent Actions requests by status code, covering 401s, 403s, schema and API version errors, usage limits, and silent no-ops.
Agent Actions introduction
Get to know Agent Actions and how to start using them.
Agent Actions
Reference documentation for the Agent Actions HTTP API.