June 27, 2023 (Updated September 27, 2023)

Getting started with AI Assist for Sanity Studio

By Martin Jacobsen

Sanity AI Assist puts the power of Large Language Models (LLM) right at your fingertips in the studio, where your content lives. Write reusable instructions in natural human language to a document-aware AI assistant that can handle chores and repetitive tasks while you focus on the creative stuff.

In this guide, we will get familiar with how AI Assist integrates into Sanity Studio and explore some example instructions to give you all you need to put these new capabilities to work in your own projects.

Gotcha

This article is about a feature that is currently available on all paid plans from Teams and up.

We all spend an astonishing amount of time on stuff that isn’t, in fact, about content creation or refinement but rather the type of repetitive chores and menial drudgery that seem to pop into existence in the orbit of any creative task.

Write a one-paragraph summary of your article for the front page, then cut it down further to fit in a tweet. Come up with three alternate titles for A/B testing. Pick the right category and tags for the algorithms. Extract all the quotes for fact-checking. All the little stuff that can’t easily be automated but feels like it should be.

Sanity AI Assist is here to take some of that dreary busywork off your hands, leaving you to spend your time and energy tackling more interesting and stimulating problems. If you want to get right into the inner workings and see some sample instructions, jump to the section titled The rules of AI Assist, or continue reading for some examples of usage and a step-by-step tour of the feature with a practical example.

What does it do?

Before we get into the grand tour of the AI Assist feature, you might want to see some examples of it in action. Let’s start with a simple but powerful example of giving the AI some input and asking it to iterate on it to create variant titles for an article. We’ll be re-creating this example in the section next section – Touring the AI Inspector with a practical example.

Take my idea and run with it

The simplest tricks are often the best. Give the assistant some input and tell it to riff on it. Massage your input and rinse and repeat for a fresh take. Sanity AI Assist takes it one step further by integrating intimate knowledge of your document structure and ability to format its conjurations accordingly.

Paste from wherever and format it my way

Tasks that no human gets excited about – like taking an unstructured blob of input and fastidiously sorting, categorizing, and entering each element into the appropriate custom block component – are what AI Assist thrives on.

Structuring content in stride

The dirty secret of Large Language Models is that their so-called ‘intelligence’ is really just being very good at recognizing and imitating structure in language. We can put that to our advantage by feeding it raw input and asking it to find and extract certain types of content. For example, check out how Sanity co-founder Simen Svale created a new content type, “facts,” and used Sanity AI Assist to extract the facts from an article and drop them into the “facts” field, ready for verification of truthiness.

Touring the AI Inspector with a practical example

In this section, we’ll get familiar with Sanity AI Assist and how it fits in your studio workflow by re-creating and then expanding upon a basic instruction to generate a list of alternative titles for an article.

The AI Inspector is shown open in the right-hand side panel and focused on the Alternate titles field.

Our example document has a very minimalist schema with a Title string field and an array of strings for Alternate titles. You can reproduce this example with any document that has a title and a field that has some way to output a list, such as Portable Text or even plain text.

Re-creating the ‘Alternative’ titles instruction, step by step

Protip

If you prefer action to reading, look for the pointing finger emoji 👉 to quickly locate the next actionable item!

👉 From any open document in the editor pane, open the AI Inspector by clicking the sparkle icon ✨ in the document’s top right context menu. A new panel on the right side of the document pane should open.

We didn’t make the rule that says :sparkle: is the AI emoji, but we do follow it!

If you are using AI Assist for the very first time, you may be prompted to enable the feature by creating a token. This step must be completed once by a user that has token creation privileges, typically an admin or developer type of role.

The AI Inspector will open with the entire document as its “target” because it was opened from the root-level sparkle button in the top right corner of the editor. To target a specific field, hover your cursor over the field to reveal its very own dedicated sparkle button. Then click it to switch the context to that field.

👉 Click the sparkle ✨ button to focus the AI Inspector on the Alternate Titles field.

Click the icon next to any AI-enabled field to focus the AI Inspector panel on it.

Once you have the AI Inspector panel focused on the Alternate Titles field, you are ready to create your first instruction. Let’s start by clicking the button labeled Add item, and an empty instruction will open in the editor. You can also decide whether to share your work with other users of this studio or keep it private for now.

👉 Create a new instruction by clicking Add item. Give your instruction an appropriate and informative name, e.g., Create variants, and then move on to the Instruction body.

Sadly, theres no AI Assist to help you write instructions for AI Assist. Yet.

Instructions for AI Assist are written in a normal, human-readable style. These instructions can be enhanced by adding references to key values from the current document, which the assistant can access in real-time. You can also prompt the user for input or refer to reusable contextual documents – such as a style guide or a description of your target audience – to further inform the assistant about the expected output. We will look closer at each of these options in due course, but for now, let’s go with the first option and select Document field.

👉 In plain English, instruct the assistant to generate some alternate titles, then tell it to base its suggestion on the current value of the Title field by clicking the + in the upper left corner of the Instructions textarea and selecting Document field. In the popup that appears, search for Title to find and select the appropriate field from your schema

The instruction is saved automatically whenever you make changes and is now ready to run. You can close the AI Inspector and invoke the instruction from the sparkle ✨ button on the Alternate titles field. Alternatively, you can keep the sidebar open and run the instruction from there by clicking the Run instruction button at the bottom of the AI Inspector interface. Just make sure you’ve actually entered a Title for it to base its mad libs on!

👉 Run the instruction and let the assistant do its thing. You’ll see the results unfold in real-time.

You won't believe how helpful AI Assist is!

The assistant will notice that you are targeting an array and format its output accordingly. You could also have it output its result as a bulleted list in a Portable Text field or in a plain text format like markdown. You can experiment with how you phrase your instruction to generate a different flavor of clickbaity titles – something we’ll explore further in the next section!

Making it interactive – Prompt for User Input

Let’s explore the other possibilities presented by that unassuming + in the top left corner of the Instructions input area. You could keep editing your current instruction but let’s instead preserve our work and create a copy to keep working on by navigating back to the list of available instructions for the Alternate Titles field and choosing the Duplicate option in the contextual menu hidden under the three vertical dots on the Create variants instruction listing.

The AI Inspector should open your new copy in the editor automatically. Make sure to rename it to something appropriate, like Create targeted variants.

👉 Edit your instruction so it looks like the leftmost figure. Then click the + button and select User input. Fill in the details to your liking, as in the middle figure. Then, finally, close the popover dialog so your instruction looks like the rightmost figure.

👉 Start up the assistant by clicking Run instruction. You should see a dialog prompting you for some target audience groups.

We’ve emptied out the Alternative titles array in between for the sake of tidy screenshotting, but you can keep piling them as high as you like.

👉 Enter a comma-separated list of target audiences, such as Boomers, GenX, Millennials, Zoomers. Then submit the dialog and wait for the assistant to complete the instruction. Your result should resemble the screenshot below.

It’s worth mentioning at this point that even though we are sticking to outputting strings into an array for the sake of brevity and accessibility in this section, the assistant would happily populate a more complex field, like an array of objects with fields for title, target, and emoji.

Don’t repeat yourself - Use Contexts to prime your instructions

Let’s take a final detour to get familiar with Contexts. The final input type in the + menu triumvirate requires you to do some prep work first, but it is quite the power feature once you get going! Contexts are reusable bodies of text you can use to prime your assistant with additional … well, context. A company style guide would be a good candidate for a context, as would any large-ish amount of textual background information you would like your assistant to keep in mind.

👉 Navigate to the leftmost pane in your studio titled Content. You should see a new entry in your list of document types titled AI Context. Select this new option, and click the Create new button in the top area of the new pane. (It’s the one that looks like a little pencil writing in a square box.)

👉 Give your new context a name, and start entering some background info you’d like to include in your instructions. Or, just copy and paste the example below to fast-track the tutorial. Your context is saved automatically, so there’s no need to click a publish button. Once you’re happy, move on to the next step.

You are a copywriting assistant whose specialty is coming up 
with copy to entice the readers to click your story. You work 
for the most popular news outlet in the Galactic Empire around 
the time of the events of the movie Star Wars: The force awakens 
take place. All your historic references will be checked and
approved by Empire fact-checkers to make sure the Empire 
always looks good and heroic, and any rebel activity is 
portrayed as heinous terrorism.

👉 Navigate back to your article document, or create a new one. Then open the AI Inspector and focus the pane on the Alternate titles field once again. Make another copy of the original Create variants-instruction as earlier, and name your new instruction Create contextual variants.

👉 Edit your instruction to look like the screenshot above. Then click + to open the insert menu, and this time choose AI Context. Start typing the name you gave your context in the popover dialog that should appear, and select your context. Refer to the figures below if needed.

👉 Once more, make sure your document has a Title, then run your instruction either by clicking Run instruction in the AI Inspector or by finding it in the AI contextual menu (the sparkle ✨ button) for the Alternate titles field.

Empire did nothing wrong!

It’s no protocol droid, but it’s still pretty neat! Let’s wrap up this tour of the AI Assist interface with a list of some main takeaways:

  • AI Assist instructions can be attached to a specific field or to the document as a whole
  • You write instructions in plain language, and you can reference any field’s value from the current document in your instruction
  • You can also prompt for user input and include contexts – larger bodies of text that can be reused between instructions as additional setup and background for the assistant

The rules of Sanity AI Assist – What can and can’t it do?

AI Assist can be disabled for any field through configuration. By default, AI Assist is enabled for most object, array, and string field types. Your assistant can write to all compatible fields that it detects. Additionally, it can create array items, including Portable Text blocks.

Number fields are not supported and will not be changed by the assistant.

Protip

Large Language Models (LLM) are still new tech! The constraints and limitations are still being explored, but some common caveats to the field in general that you might run into using AI Assist are:

  • Instruction complexity: putting long instructions on deep content structures may exhaust the model capacity
  • Timeouts: To be able to write structured content, we're using the largest language models - long-running results may time out or intermittently fail
  • Capacity: The underlying APIs we are using are resource constrained

What can you reference in your instructions?

  • Document field – You’ll be using this a lot. Use this to refer to fields in the same document. Note: you can refer to the current field you are writing the prompt for - you need to do this to have the assistant complete arrays and portable text using the content that’s currently there.
  • Context – This is a globally available text fragment that you can use across prompts. Use it to add reusable information to instruct your assistant, like style guides or prompts to write for a certain demographic or language.
  • User input – Ask the user for input every time the prompt runs. For example, if you frequently write articles based on interview transcripts, you could use the transcript as User Input and the style guide of your publication as Instruction Context.

What can you generate?

You can add instructions to different levels of the document to have the machine work there.

  • Single value: Attach an instruction to strings like titles and ledes to have the assistant come up with content there. Here it will just replace existing content.
  • Arrays and rich text: Attach instruction to have the assistant add items to an array.
  • Objects: Attach instruction to have the assistant add content to all fields within it.
  • Entire documents: Give the machine some autonomy! Attach instructions to the entire document and get rough drafts for everything. You tell it what you want it to fill in and what to ignore and what block types you want generated.
    • “Based on [Title] and [Lede] write some tags and suggest some copy for the body field with an emphasis on timelines” is a valid prompt.

Example prompts

Write more of a body

Where: body Portable Text or plain text field

Prompt:

Given the title body keep writing copy.

Get started on an article

Where: document instruction

Prompt:

Given the following inspiration User input (What should the article be about?) Create an article.

Summarize a field

Where: summary Portable Text or plain text field

Prompt:

Summarize body

Variation (to suggest how the summary should be formatted when saving to a formatted text field):

Summarize body. Do not use lists, headlines, or quotes.

List categories

Where: On an array field with strings

Prompt:

List the categories relevant to body

Shorten a field

Where: On the field to shorten

Prompt:

Shorten <the field itself>

Translate a field

Where: On the field where the translation should be stored

Prompt:

Translate title to norwegian.

Sentiment analysis

Where: On a text field where the sentiment should be stored

Prompt:

Classify the sentiment of the following text body

Determine if it is elated, happy, neutral, sad or angry.

Create a catchy title

Where: On the title field

Prompt:

Create a catchy and engaging headline about User input: (What should the title be about)

Smart” paste

Where: On any field or at the document level

Prompt:

Given the following User input:

User input (Paste your stuff here)

Format the User input so it aligns with the schema. Do not omit sections or paragraphs, but use formatting, headings and lists as appropriate.

Infer item types based on context.

Tips & Tricks for prompting

  • The assistant knows about the schema. When running instructions at the document level, for instance, you can still target specific fields by referring to them by title. E.g., “Create a description based on title, then add a relevant callout to body. Do not add anything to any other fields.”
  • Using explicit commands and clear language like “Important: <command>” can help guide the assistant in a certain direction if it’s being too creative.

Third-party sub-processors

This version of the feature uses OpenAI.com as a third-party sub-processor. Their security posture has been vetted by Sanity's security team, and approved for use.

Sanity – build remarkable experiences at scale

Sanity Composable Content Cloud is the headless CMS that gives you (and your team) a content backend to drive websites and applications with modern tooling. It offers a real-time editing environment for content creators that’s easy to configure but designed to be customized with JavaScript and React when needed. With the hosted document store, you query content freely and easily integrate with any framework or data source to distribute and enrich content.

Sanity scales from weekend projects to enterprise needs and is used by companies like Puma, AT&T, Burger King, Tata, and Figma.

Other guides by author