Blog
4 min readtutorialnextjsintegration

How to Add AI to Your Next.js Documentation

Add an AI agent to your Next.js docs so it answers from your own content with citations — loaded the App Router way, without hydration issues. Here is how.

Adding an AI agent to a Next.js documentation site lets readers get answers from your own content with citations — and the trick is loading the widget the App Router way so it doesn't cause hydration warnings or get stripped by the build. The setup is quick once you use the right loading pattern.

Why Next.js docs benefit

Many teams build custom documentation in Next.js for full control over design. That content is ideal for an AI agent: readers want direct answers, not a search page. An agent reads your docs and responds with the answer plus a citation. The cleaner your content's structure, the better it retrieves. See the documentation use case.

Step 1: Index your content

Create an account, get your publishable site key, and point the platform at your deployed Next.js docs to crawl, or connect the repository. The platform chunks and embeds your content for retrieval at answer time. Schedule a re-crawl so updates keep the agent current.

Step 2: Load the widget with next/script

This is the step that matters in the App Router. Add the AIML.chat embed using the Script component with a client loading strategy in your root layout, so it runs after hydration rather than during server render. That avoids React hydration mismatch warnings and ensures the widget loads reliably. Keep your publishable key in a NEXT_PUBLIC_ environment variable. The Next.js setup skill walks through it.

Step 3: Mind the segment config

A Next.js gotcha worth knowing: route segment config like export const revalidate must be a literal value, not an imported constant, or the build fails. It's unrelated to the widget itself but trips people up when wiring docs pages. Keep those literals literal.

Step 4: Configure a technical agent

Choose a technical agent for precise, cited answers that include the relevant snippet. Give it a grounded prompt: answer only from indexed docs, cite sources, escalate when unsure. Grounding is what keeps it accurate — see what is RAG.

Step 5: Structure and measure

Keep docs pages focused with clear headings and descriptive titles so they retrieve well, and split long pages. After launch, use the analytics to find the questions the agent couldn't answer — your docs backlog. Re-crawl after writing and the agent improves.

What you end up with

Your Next.js docs now answer questions directly, with cited responses, around the clock. Developers get unblocked without a ticket, and you see where the docs are thin — all from a Script component and an indexed crawl. See how to add AI agents to Docusaurus for a framework comparison.

Why grounding beats a generic widget

Whatever platform you are on, the value is not the chat box — it is that the answers come from your own content. A generic chatbot bolted onto Next.js will improvise from a general model and confidently get your details wrong, which erodes trust fast. A grounded agent answers only from what you indexed and cites the page, so every reply is traceable to something you actually published. That is the difference between a widget that frustrates visitors and one they come to rely on, and it is why indexing your real content is the step that matters most.

Getting started

Add an agent to your Next.js docs on the AIML.chat free plan: index your content, load the widget with next/script, and configure the agent. Compare tiers on the pricing page.

Will it cause hydration warnings?+

Not if you load it with next/script using a client strategy in the root layout, so it runs after hydration rather than during SSR.

Where do I put my site key?+

In a NEXT_PUBLIC_ environment variable. The publishable key is meant to be client-side, but keeping it in env keeps your code clean.

Will it stay current?+

Yes — schedule a re-crawl so each docs update re-indexes and keeps the agent accurate.

Get the AIML.chat newsletter

Practical tips on AI docs assistants, RAG, and growing with content — a couple of emails a month. No spam, unsubscribe anytime.

Double opt-in — we'll email you to confirm.

Add AI chat to your site

Free plan · No credit card · 5 minutes

Get started free