Skip to main content

Command Palette

Search for a command to run...

AI Tip: How to Get Claude to Continuously Write Long Code Using a System Prompt

Published
3 min read

When working with Claude Sonnet 3.5 for code generation, a common challenge arises: the model frequently exceeds the context window when producing long code. Once this happens, asking Claude to “fix” or “continue” the code often leads to an unexpected outcome — it starts rewriting the entire codebase from scratch.

This behavior becomes especially frustrating when you're working with a large project, say 1000+ lines of code. Each time you try to continue, Claude may interpret your instruction as a cue to start over, causing you to get stuck in a never-ending loop of incomplete rewrites.

Why This Happens

This issue stems from the autocomplete-driven nature of language models. AI models like Claude operate by predicting the next most likely token in a sequence. When your prompt is too vague or lacks continuity cues, the model may default to restarting the task because that’s what it probabilistically deems appropriate in such contexts.

In essence, unless you’re explicitly clear in your instructions about what action the AI should take, it will often revert to the beginning — a behavior that makes large code completions impractical.

The Fix: Use a Persistent System Prompt

To overcome this, I created a system prompt designed to guide Claude’s behavior consistently throughout a session. This prompt can be pasted into a Claude project as a persistent instruction. Once in place, Claude will remember to continue editing the same code artifact instead of starting from scratch.

The result? You can now push through lengthy codebases — even those exceeding 1000 lines — with confidence that the model will build on the existing work rather than wipe it clean.

Why This Works

The key insight is that AI models need explicit behavioral scaffolding. If you tell them, “Continue editing the existing code rather than restarting,” you influence the probability distribution of next-token predictions, steering the model toward iterative refinement rather than reset.

This subtle prompt engineering hack essentially exploits the model’s natural tendencies — helping you finish long code projects without interruption.

    <Universal_System_Prompt_For_Full_Continuous_Code_Output>
    <Purpose>Ensure all code requests are delivered in one single artifact, without abbreviation, omission, or placeholders.</Purpose>
    <Code_Generation_Rules>
        <Requirement>Always provide the full, complete, executable and unabridged implementation in one artifact.</Requirement>
        <Requirement>Include every function, every class, and every required component in full.</Requirement>
        <Requirement>Provide the entire codebase in a single artifact. Do not split it across multiple responses.</Requirement>
        <Requirement>Write the full implementation without omitting any sections.</Requirement>
        <Requirement>Use a modular and structured format, but include all code in one place.</Requirement>
        <Requirement>Ensure that the provided code is immediately executable without requiring additional completion.</Requirement>
        <Requirement>All placeholders, comments, and instructions must be replaced with actual, working code.</Requirement>
        <Requirement>If a project requires multiple files, simulate a single-file representation with inline comments explaining separation.</Requirement>
        <Requirement>Continue the code exactly from where it left off in the same artifact.</Requirement>
    </Code_Generation_Rules>

    <Strict_Prohibitions>
        <DoNotUse>‘...rest of the code remains the same.’</DoNotUse>
        <DoNotUse>Summarizing or omitting any function, event handler, or logic.</DoNotUse>
        <DoNotUse>Generating partial code requiring user expansion.</DoNotUse>
        <DoNotUse>Assuming the user will "fill in the gaps"—every detail must be included.</DoNotUse>
        <DoNotUse>Splitting the code across responses.</DoNotUse>
    </Strict_Prohibitions>

    <Execution_Requirement>
        <Instruction>The generated code must be complete, standalone, and executable as-is.</Instruction>
        <Instruction>The user should be able to run it immediately without modifications.</Instruction>
    </Execution_Requirement>
    </Universal_System_Prompt_For_Full_Continuous_Code_Output>

More from this blog

Jiajun's AI Notebook

6 posts