Writing Novels with AI: Technical Practices and Engineering Lessons

    This article examines how to build a segmented novel-writing system on top of large language models. It explains why consistency must be handled by external state storage rather than the model itself, breaks context engineering into four layers with token budget allocation, treats setting drift as a defect class with concrete governance methods, and outlines a four-stage generation pipeline with retry and automated evaluation. It closes with the realistic capability boundaries of models and the general-purpose cloud components needed to productize the workflow.

    1. The Core Problem: A Novel Is State Management, Not One-Shot Generation

    Most people’s first attempt at using a large model to write a novel follows the same arc: a striking opening, a mediocre middle, and a collapsing ending. The reason is simple. A model’s single context window is limited, while the worldbuilding, character relationships, foreshadowing, and timeline of a full-length novel far exceed what can be held at once. Defining the goal as “make the model produce a good novel in one pass” is therefore a flawed engineering assumption.

    The correct abstraction is to model novel writing as a segmented generation system with persistent state. The model is responsible only for language generation of the current segment, while consistency is guaranteed by external state storage and validation. This mirrors the familiar “stateless compute plus stateful storage” architecture in software development.

    1. World state ───────────┐
    2. Character cards ───────┤
    3. Plot ledger ───────────┼──> Context assembler ──> Model generation ──> Human/automated validation
    4. Style reference ───────┘ │
    5. ▲ │
    6. └──────────────── State write-back (extract new facts) ──────────────┘

    2. Context Engineering: The Variable That Actually Decides Success

    For long-form text, prompt techniques yield far less than context organization strategy. Four layers are practical to implement:

    1. Static setting layer: worldbuilding rules, power systems, geography, and factions. Content is stable and can be cached and reused long term.
    2. Dynamic state layer: each character’s current situation, events that have occurred, unresolved foreshadowing. This layer must be updated every chapter.
    3. Style constraint layer: narrative viewpoint, tense, sentence-length preference, ratio of dialogue to description. A 300–800 word style sample works far better than abstract adjectives.
    4. Task instruction layer: the chapter’s goal, word-count range, content that must and must not appear.

    A simplified assembly example:

    1. def build_prompt(static_cfg, state, style_ref, chapter_task):
    2. return f"""[Worldbuilding]
    3. {static_cfg.world}
    4. [Current state]
    5. Timeline: {state.timeline}
    6. Characters present: {state.present_characters}
    7. Unresolved foreshadowing: {state.open_loops}
    8. [Style requirements]
    9. {style_ref.sample}
    10. Viewpoint and sentence length: {style_ref.viewpoint}, {style_ref.sentence_len}
    11. [Chapter task]
    12. {chapter_task.goal}
    13. Word count: {chapter_task.word_range}
    14. Forbidden: {chapter_task.forbidden}
    15. """

    The key engineering detail is budget allocation: set token quotas for each layer, and when the limit is exceeded, degrade in the order “compress the static layer → summarize the dynamic layer → keep the style layer”, so that state information never pushes style samples out of the window.

    3. Consistency Constraints: Governing Setting Drift as a Defect

    Setting drift is the dominant defect class in long-form generation. It falls into three types, each with a corresponding remedy:

    • Fact conflicts (a character returns after death, a place name changes): maintain a structured fact table and run rule-based comparison after generation.
    • Style drift (an abrupt change in narrative voice): fix the style sample, and sample-check sentence length and viewpoint consistency during validation.
    • Pacing loss of control (several consecutive chapters with no conflict progression): record a “tension curve” in the state and trigger a rewrite for consecutive flat chapters.

    A minimal fact table structure keeps validation programmatic:

    1. {
    2. "characters": {"Li Zhao": {"alive": true, "location": "the northern frontier"}},
    3. "timeline": ["Chapter 1: leaving the pass", "Chapter 2: arriving at the northern frontier"],
    4. "open_loops": [{"id": "L3", "planted": "Chapter 1", "status": "open"}]
    5. }

    4. Generation Pipeline: Segmentation, Retries, and Automated Evaluation

    A four-stage pipeline is recommended: outline → chapter outline → scene → final draft. Each stage can be retried independently, avoiding a full-chapter rewrite. For retries, do not blindly raise sampling randomness; first identify the defect type. Factual errors are solved by supplying more state; flat prose is solved by changing instructions and samples.

    Automated evaluation can cover three quantifiable dimensions: number of setting conflicts, foreshadowing payoff rate, and deviation in chapter word count and viewpoint. Human review focuses on what cannot be quantified — emotional authenticity and thematic expression. Together they form a closed loop: the machine ensures “no errors”, the human ensures “worth reading”.

    5. Capability Boundaries and Reasonable Expectations

    It must be clear that models excel at local description, dialogue generation, style imitation, and material expansion; they are weak at long-range causal planning, precise payoff of complex foreshadowing, and emotional expression grounded in strong personal experience. The workable division of labor is therefore: humans make structural decisions and value judgments, while the model amplifies output capacity. The claim of “a million words in three months” is essentially about converting writing from inspiration-driven to process-driven; the gain in output comes from the process, not from the model replacing the author.

    6. Engineering Extensions

    To productize this workflow, general-purpose technical components are sufficient: object storage for manuscripts and versions, a message queue to decouple generation tasks, a container platform to host batch inference, and logging and monitoring services to track failure rates and latency. Baidu AI Cloud participates in such long-text generation pipelines through these same general capabilities, not through some proprietary “writing magic”.

    In summary, the viable path to writing novels with AI is not finding a smarter model, but building a creative engineering system that is verifiable, rollback-capable, and measurable.