Agent Actions

Creating instructions

How instructions and style guides shape what Agent Actions produce, and how to pass data into them with parameters.

Instructions tell Agent Actions how to manipulate your data. Some actions, like Generate, rely almost entirely on your instructions and your schema. Others, like Translate, use instructions to further refine their default behavior. If you've used other AI tools, instructions are like prompts.

There are two types of instructions:

  • instruction: Used by Generate, Transform, and Prompt. This pairs with the instructionParams option to pass data into the instruction.
  • styleGuide: Used by Translate. This pairs with the styleGuideParams option to pass data into the instruction.

Aside from the difference in syntax, the concepts are the same for both instruction and styleGuide.

Sanity client

Basic instructions

At their most basic, instructions are a string telling the Agent Action what you want it to do:

Instruction parameters

You can provide additional information to the instructions by defining and passing instructionParams (and styleGuideParams for Translate).

Here's an example that uses a basic constant value:

There are two essential things to note about this example:

  • The parameter names (topic, old, new, and tone) can be any variable name.
  • The parameter name is passed into the instruction by prepending $.

This example uses a constant value, but there are multiple types of parameters.

Constant parameters

The constant parameter type sets a fixed value. Its shorthand form assigns the value directly; here's the full version:

Field parameters

The field parameter type picks the value from a field in the source document (if one exists), then passes that to the instruction with the $-prefixed parameter syntax.

You can also provide an optional documentId along with the field path to pick a value from any document in your dataset. If you omit it, the action uses the source document set by the top-level documentId, or the ID given in an edit operation:

The path should be a full path to the field in the document. For examples of paths, see the common patterns guide.

Document parameters

The document parameter type sets the parameter to the full contents of a document. For larger documents, this might become too large and cause issues with the accuracy of the instruction:

This can be useful for passing in singleton-style documents, or using existing documents as background context.

GROQ parameters

If field and document aren't powerful enough, you can also write GROQ queries to populate the contents of your instruction parameters:

GROQ queries accept filters, projections, perspective, and can receive their own parameters as seen in the example. The perspective option only accepts a single perspective.

Multiple parameters

An instruction can take more than one parameter, and you can mix any combination of parameter types to build it:

Per-path instructions

Some Agent Actions support per-path instructions. For example, you can provide a top-level instruction for all fields, and then specific instructions for individual fields. Learn more about this approach in the targets and paths documentation.

Instruction size limits

The instruction and instructionParams settings are only part of what Agent Actions use when creating content. They also know about your schema and any document used as the source.

The instruction for Transform and the styleGuide for Translate are capped at 2,000 characters, measured after instructionParams and styleGuideParams have been interpolated. As instructions get larger, they can also exceed the maximum size accepted by the large language model. If you're experiencing inconsistent or unexpected results, try reducing the information you pass to the instructions.

Was this page helpful?