Prompt Engineering: The Coherent Author (An Autonomous Writing Agent)
We've built agents that are specific (explicit instructions), structured (structured output), logical (chain-of-thought), and can even use tools to find new information. But they have one final, hidden flaw: they have a short memory.
This post is for you if you've ever asked an LLM to "write a 10-page report" and found that by page 8, it has completely forgotten its main thesis from page 1, contradicts itself, and rambles endlessly.
Today, we'll build a Long-Form Writing Agent and use a hybrid of techniques—Meta-Prompting and Thread-of-Thought (ThoT)—to ensure our bot stays on-topic and coherent from beginning to end.
The problem: the Amnesiac Author
An LLM's "memory" is its context window. When you ask it to write a 10,000-word story, the words from the beginning of the story "fall out" of its memory by the time it gets to the end.
Use Case: "Write a 5-section report on the history of renewable energy."
graph TD
A["User: Write a 5-section report"] --> B(LLM)
B --> B1["Section 1: Solar power is the future"]
B1 --> B2["... writing 2000 words ..."]
B2 --> B3["Section 5: Solar power is inefficient and impractical"]
B3 --> C[Bot: Generates the report]
style B3 fill:#ffebee,stroke:#b71c1c,color:#212121
Why this is bad: The bot has lost the "plot". It forgot its own thesis and contradicted itself. The long-form output is unusable.
Improvement 1: Ask the AI for the plan (Meta-Prompting)
Before we ask the bot to write, let's ask it to create a plan. But instead of just asking for a plan, let's use Meta-Prompting to ask the LLM to generate the best possible prompt for this task.
We are asking the AI to help us prompt it better.
The "How":
# This is a "Meta-Prompt" - a prompt to generate a better prompt.
meta_prompt = """
I need to write a 5-section report on 'The History of Renewable Energy'.
I want an LLM to generate a high-quality, comprehensive, and
well-structured outline for this report.
Please write the *actual prompt* I should use to get this
high-quality outline. Make the prompt very detailed.
"""
# The LLM's response to our meta-prompt:
llm_generated_prompt = """
Generate a 5-section outline for a report on 'The History of Renewable Energy.'
The outline must be structured as a JSON object.
For each section, provide:
1. `title`: A clear, descriptive title.
2. `main_points`: A list of 3-4 key sub-topics to cover.
3. `thesis`: A one-sentence thesis for that specific section.
...
"""
Observation: The prompt the LLM generated for us is far more detailed than what we would have written. We can now use this prompt to get a perfect, structured outline.
Improvement 2: Guide the flow (Thread-of-Thought)
Now that we have a high-quality outline, we can solve the "amnesia" problem.
Instead of asking the LLM to write the entire report at once, we will use a Thread-of-Thought (ThoT) approach. We will loop through our outline section by section, feeding the previous sections back in as context.
This maintains a coherent "thread" and ensures the LLM never forgets its main points.
The "How":
# 1. Get the plan (using our Meta-Prompt from above)
outline_json = ... # {"sections": [{"title": "1. The Dawn of Hydropower"}, ...]}
# 2. Loop through the plan, section by section
final_report_sections = []
for section in outline_json["sections"]:
# Create a prompt for this *specific* section
section_prompt = f"""
You are writing a report on 'The History of Renewable Energy'.
Here is the overall plan: {outline_json}
Here is the text of the report you have written SO FAR:
{ " ".join(final_report_sections) }
Now, please write the *next* section: '{section['title']}'
Ensure it logically follows the previous text and builds
on its ideas.
"""
# 3. Call the LLM to write *only* this one section
section_text = llm.chat(section_prompt)
final_report_sections.append(section_text)
# 4. Stitch the full report together
full_report = "\n\n".join(final_report_sections)
graph TD
A[Start: Get Report Outline] --> B[Loop: Section 1]
B --> C[LLM]
C --> D[Generated Text: Section 1]
D --> E[Loop: Section 2]
E --> F[LLM]
F --> G[Generated Text: Section 2]
G --> H[Loop: Section 3]
H --> I[LLM]
I --> J[Generated Text: Section 3]
J --> K[Final Report Coherent]
style K fill:#e8f5e9,stroke:#388e3c,color:#212121
Observation: This hybrid approach is the pinnacle of long-form generation.
- Meta-Prompting gave us a high-quality, structured plan.
- Thread-of-Thought ensured the LLM maintained a "memory" by feeding back its own previous writing, guaranteeing a coherent flow from start to finish.
Challenge for you
-
Create a hybrid agent: Combine the techniques from our step-back and goal decomposition, self-critique, and ReAct agents posts.
-
The goal: Build an "Autonomous Blog Post Writer."
-
The process:
- Step 1 (Meta-Prompting): Ask the LLM to generate a prompt to create a blog post outline.
- Step 2 (ReAct): For each section of the outline, use a
search_toolto find 3-4 supporting facts. - Step 3 (ThoT): Write the blog post section-by-section, injecting the facts from the ReAct step as context.
- Step 4 (Self-Critique): After the full draft is done, ask the LLM to critique and refine its own post for "flow and engagement."
You've just designed the logic for a truly advanced autonomous agent. For more on building production agents, see our guides on architecting AI agents and advanced RAG systems.
Key takeaways
- Context windows have limits: Long-form content exceeds LLM memory, causing coherence problems
- Meta-prompting improves planning: Asking the LLM to design its own prompt produces better structured outlines
- Thread-of-thought maintains coherence: Writing section-by-section with context from previous sections ensures consistency
- Hybrid techniques multiply power: Combining meta-prompting, ReAct, ThoT, and self-critique creates production-ready writing agents
For more on building production AI systems, check out our AI Bootcamp for Software Engineers.