📆 Don't miss our Sanity Showcase virtual meetup on March 20th!

Best practices for AI-enhanced Sanity development

Learn how to enhance AI-generated Sanity code with best practices that improve upon default configurations, creating better experiences for content creators.

Because Sanity is an unabashedly all-code tool, you can use AI to rapidly configure a new Sanity Studio or integrate Sanity Client into your application faster than low-code alternatives requiring hundreds of in-browser clicks.

However, without proper guidance, AI typically produces average code that relies on defaults—resulting in suboptimal user experiences for your content creators.

AI tools can dramatically accelerate Sanity development, but without proper guidance, they often produce generic code that fails to leverage Sanity's full capabilities. This guide bridges that gap, teaching you how to instruct AI to generate Sanity code that follows established best practices while maintaining the speed advantages of AI assistance.

While most models understand all the APIs Sanity makes available, they are unlikely to follow opinionated best practices, which we have published over the years in guides and courses on Sanity Learn.

This guide will help you:

  • Set up AI tools to generate high-quality Sanity code
  • Avoid common pitfalls of AI-generated configurations
  • Implement best practices from Sanity Learn into your AI workflow
  • Create better experiences for your content teams

Implementing AI Rules and documentation

You can add these rules to your project to ensure any generated code follows our published best practices. We will continue to iterate on these files over time.

Implementing rules for AI code generation is different for each IDE

Setting Up AI Rules

The sanity-io/ai-rules repository contains a deliberately short, manually curated .mdc file with most of the opinionated best practices for writing Sanity Studio configuration and GROQ queries.

We intend to keep it updated as we discover new rules, so it might be worth revisiting it from time to time—we also welcome PRs.

Cursor

Others (VS Code, etc)

  • Add the rules file to your project as a .txt file and refer to it when prompting new Sanity code

Leveraging Sanity Learn Content

All course and lesson material on Sanity Learn is automatically kept updated and made available in an LLM-friendly format thanks to Portable Text being easily serialized to the llms.txt standard. You can read how we made this on our blog.

We are working on making the documentation available as well.

There are two different sizes you can import into your IDE:

  • /llms.txt is an abbreviated index of all the content with links
  • /llms-full.txt is the complete content (sometimes optimized to fit within the context window limits)

Cursor

  • Within Cursor chat, type "@Docs," select "Add new doc" and then paste in https://www.sanity.io/learn/llms.txt (or the filename of your choice above)
  • You can also add https://www.sanity.io/docs to the Cursor Settings -> Features -> Docs configuration.

Others (VS Code, etc)

  • At this time, there is no documented support for the llms.txt standard. We will update this page as it is made available.

Was this article helpful?