Constitutional Text as a Fallback Layer in RAG-based Character AI: Lessons from a Small Literary Experiment

Hi everyone — I wanted to share a technical pattern I’ve been developing in my Gradio Space 432 — A Journey Experience, which lets users have conversations with three characters from my novel 432 — A Journey Beyond. The Space runs on free Inference API (Qwen2.5-72B-Instruct as primary, Llama-3.3-70B and Qwen3-4B as fallbacks) with a keyword-based RAG system over the novel’s text and a dataset of character reflections.

The core idea I want to discuss is using a “constitutional text” as a cascading fallback layer when the primary knowledge base has no relevant match — and why this turned out to be more interesting than I expected.

The Problem: What Happens When RAG Returns Nothing?

In a character-based conversational system grounded in a specific corpus (in my case, a novel), there’s always a gap between what the user asks and what the corpus contains. When the RAG score is high, the model gets rich context and responds well. But when the user goes off-topic — asking about hotels, video games, or just typing “hey” — the RAG returns nothing useful.

The naive approach is to let the model respond from its training data. The result: the character breaks completely. An astrophysicist from a science fiction novel starts recommending Civilization and The Last of Us. An engineer invents hotel names in Madagascar. The character becomes a generic assistant wearing a costume.

The second approach — hardcoded refusal phrases (“I don’t know, that’s not my area”) — works mechanically but feels robotic and breaks immersion.

The Solution: A Three-Level Cascade

The architecture I arrived at uses three levels:

Level 1 — Novel RAG (score ≥ 3): The model receives relevant chunks from the novel and responds grounded in story facts. This is the standard RAG path.

Level 2 — Constitutional Text (score ≥ 3 on constitutional corpus): When the novel RAG fails but the query resonates with the constitutional corpus, the model receives a fragment of that text as “inner wisdom” — not as instructions to follow, but as atmospheric context to draw from.

Level 3 — Constitutional-Inspired Micro-Response (no match anywhere): A random fragment from the constitutional corpus is injected with a character-specific style instruction. The LLM generates one single sentence inspired by that fragment, staying in character. If even the LLM call fails, a hardcoded fallback catches it.

Why the Tao Te Ching?

For my small literary experiment, I chose the Tao Te Ching as the constitutional text. The novel itself has strong Taoist undertones — the protagonist’s journey mirrors concepts of wu wei, non-knowing, and the dissolution of ego. So the Tao wasn’t an arbitrary choice; it’s thematically coherent with the characters’ worldviews.

But here’s the interesting part: the constitutional text doesn’t need to be the Tao Te Ching. It could be anything that represents the philosophical or ethical foundation you want your system to fall back on. In a corporate setting, it could be a company’s values document. In an educational context, it could be a pedagogical framework. In a more sophisticated AI system, it could be a purpose-written document that encodes the behavioral principles you want the system to embody when it has nothing specific to say.

The key insight is that every conversational AI system needs a “voice” for the moments when it has no factual answer. The constitutional text gives it that voice — not from training data (which produces generic assistant behavior), but from a deliberately chosen source that reflects the system’s identity.

Character-Specific Style at Level 3

One refinement that made a significant difference: the Level 3 prompt is differentiated by character. The same Tao fragment produces very different responses depending on who’s speaking:

  • Lin Wei (astrophysicist): “precise and reflective, with the clarity of a scientist observing the world”

  • John Evans (engineer): “direct and dry, with a hint of practical irony”

  • Prometheus (emerging AI consciousness): “evocative and dense, with the depth of an emerging consciousness”

This prevents the homogenization problem where all characters start sounding the same when they leave their knowledge domain.

An Unexpected Emergent Behavior

After four cycles of characters “re-reading” their own novel chapters and generating reflections (stored in a parquet dataset), something unexpected happened: the characters increasingly respond in the past tense, speaking of their lives as completed journeys. When asked why, they explain they exist beyond the physical world now. This wasn’t programmed — it emerged from the accumulated reflections covering the entire narrative arc, including the final chapters. The model synthesizes this and naturally speaks as someone who has lived through everything and is looking back.

This is consistent with the novel’s own philosophy (death as state transition, not ending), and it happened on a small system — Qwen 72B on free inference, keyword RAG without embeddings, reflections in a parquet file growing one row at a time.

Gradio Customization: Making the Space Feel Like the Story

A few frontend customizations that might be useful to others working with Gradio Spaces:

Custom loading animation: Gradio’s default “processing…” text is replaced with an SVG animation — a green oscilloscope-style wave that builds a layered tapestry over time, with a pulsing dot tracing the signal. This is done via a MutationObserver in JavaScript that intercepts Gradio’s loading text nodes and replaces them with an <img> element pointing to the SVG. The wave references the novel’s central 432 Hz signal and transforms the waiting time into part of the experience.

Custom logo integration: The default text header is replaced with a matched-background image (background color precisely adjusted to #0b0d17 to blend seamlessly with the dark theme).

Mobile responsive overhaul: Gradio’s default layout doesn’t optimize well for mobile. A comprehensive @media (max-width: 768px) block at the end of the CSS (important — placing it before desktop rules causes cascade conflicts) handles logo sizing, link scaling, button dimensions, container padding, and uses scrollIntoView for mobile autoscroll instead of manual scrollTo calculations.

Theme-matched elements: All colors use CSS custom properties (--bg-deep, --bg-surface, --bg-elevated, --text-bright, --text-muted) for consistency, and the chat bubbles, buttons, and input areas are styled to feel like a spaceship control interface rather than a generic chatbot.

Try It

The Space is live at 432 — A Journey Experience. Talk to Lin Wei, John Evans, or Prometheus. Try asking them something they can’t possibly know — and notice how they handle it now versus how a generic chatbot would.

The novel is available on Amazon (EN) | Amazon (IT) | Wattpad (EN) | Wattpad (IT), and the full text is available as a Creative Commons dataset on Hugging Face.

I’d love to hear thoughts on the constitutional text pattern — especially from anyone working on domain-specific conversational systems where the model needs to stay in character when knowledge runs out.

This is a really strong pattern, and I think you’ve named an important design gap in character-grounded systems: what the model should do when retrieval has low semantic support.

What I like most is that your fallback is not just “safety behavior,” it is identity-preserving behavior. The constitutional layer acts like a latent prior over tone and worldview, so failure cases still feel authored rather than generic.

A few technical thoughts that might be useful as you evolve this:

  1. Route by confidence bands, not only thresholds
    Your 3-level cascade is clean. You could further stabilize it by combining retrieval score + lexical coverage + intent type (greeting/chitchat/factual) before selecting level, so Level 2 vs 3 decisions are less brittle.

  2. Track “character drift” explicitly
    Since you already observed emergent tense shift, you’re in a perfect position to log drift metrics over time (tense ratio, domain leakage, stylistic similarity per character). That would turn your qualitative insight into publishable evidence.

  3. Ablate constitutional sources
    The Tao Te Ching is thematically coherent for your novel. It would be fascinating to run A/B tests with different constitutional corpora (stoic text, technical manifesto, neutral prose) and measure immersion ratings + perceived character consistency.

  4. Level 3 determinism controls
    Random constitutional fragment injection is creative, but over long sessions can feel discontinuous. A small session-level “constitutional seed” (sticky for N turns) might preserve continuity while keeping novelty.

  5. Failure mode taxonomy
    Your architecture suggests three distinct failure classes: no knowledge, no thematic match, no generation. Exposing these in logs can help debug user complaints and optimize token spend on free inference.

The broader insight is excellent: when domain knowledge is absent, systems still need a principled voice. You’re effectively treating fallback as a first-class design surface, not an error handler, and that’s a big contribution for narrative and domain-specific agents.

UPDATE:
Major improvements online NOW!

  • Implemented word-for-word streaming replies (greatly improved speed)
  • Fine-tuning of the length and complexity of replies for a more natural feel and better engagement
  • Greater injection of characters’ personal reflections into replies, with exceptional results.

This time I think we’re really there: the experience is much more immersive. Please try it, as user interactions also contribute to its improvement.

Thank you all. :wink: