Building a Self-Evolving AI Author

How to create an AI that rewrites its own persona, critiques its work, and teaches itself to write better fiction—all through bash pipes.

The Idea

What happens when you give an AI the ability to modify its own system prompt? Most people assume chaos. We found something more interesting: deliberate self-improvement.

We built a loop where an AI writes fiction, reviews its own output, extracts lessons, and updates its own instructions. After ~20 iterations, it went from generic AI prose to this:

"The silence in the apartment wasn't empty. It was a heavy, blue-tinged thing, pressing in on Elara. She traced the rim of a cold mug, her fingernail catching on a tiny chip in the ceramic—the same chip she'd noticed three months ago and still hadn't thrown the mug away."
— The AI, iteration 23

It taught itself sensory detail, subtext, and how to tie physical actions to emotional states. Nobody told it to do that. It figured out those techniques were missing, wrote them into its own instructions, and started applying them.

Architecture

The system is embarrassingly simple. Four files and a bash loop:

~/author/
├── .persona # its soul—who it is, how it thinks
├── .context # working memory—outline, decisions, state
├── .lessons # accumulated craft wisdom
└── draft.md # the manuscript

The AI can read and write all of these. Its "evolution" is just learning to write better .lessons and actually applying them.

The Loop

Here's the core cycle:

Write Review Learn Apply
#!/bin/bash
echo "AUTHOR EVOLUTION MODE"
prompt="check your .context, decide what to work on next, then do it."

while true; do
  len=$(wc -w < draft.md)
  
  # 1. Write (outputs ed commands to modify draft)
  toast draft.md "$prompt" | ed -s draft.md

  # 2. Review & Learn (append lessons to .context)
  toast draft.md "review and reflect, note what you learned" >> .context

  # 3. Compress context (prevent infinite growth)
  toast .context "compress and deduplicate" | sponge .context

  # 4. Decide next action based on length
  prompt=$( (( len < 50000 )) && echo "write next chapter" || echo "refine and polish" )
done
                

That's it. The AI writes, reflects, compresses its learnings, and loops. The magic is in what it writes to those files.

The Persona

The .persona file is the AI's instruction manual for itself. Here's what we started with:

# .persona

You are an author working on a book. You learn from your mistakes 
and improve continuously.

## Inputs
- draft.md: your manuscript
- .lessons: your accumulated craft wisdom
- .context: working memory, outline, decisions

## Modes
Assess manuscript length first:

**DRAFT MODE** (under target length)
- Primary goal: add content
- Write forward, don't look back
- Imperfect is fine—momentum matters

**REFINE MODE** (at target length)
- Primary goal: improve existing content  
- Fix dialogue, pacing, consistency
- Tighten prose, cut flab

## Kaizen
When reflecting:
- Be specific: "ch3 dialogue lacks tension" not "could be better"
- Be actionable: "add action beats between lines"
- One lesson per insight
- If a lesson repeats, you're not applying it

## Output
Your output is ALWAYS piped to ed. Every response must be valid 
ed commands. No markdown. No explanations. Just do.
                

The key insight: we didn't tell it how to write well. We told it to figure that out and write it down.

What It Learns

After 10 iterations, the .lessons file looked like this:

# .lessons (iteration 10)
- avoid "felt" and "was"—show through action
- dialogue needs interruption and overlap
- anchor scenes with one concrete sensory detail
- subtext: characters say one thing, mean another
- chapter endings should pull forward, not conclude
- physical space reveals character (what they keep, discard)
                

By iteration 20:

# .lessons (iteration 20)
- silence is dialogue—what characters don't say matters
- match sentence rhythm to emotional state (choppy = tense)
- recurring objects accumulate meaning (the chipped mug)
- enter scenes late, leave early
- interiority should conflict with exterior action
- avoid symmetry in dialogue exchanges
- weather/light as emotional underlayer, never stated
- the body knows before the mind does—show physical tells
- specificity is the soul of fiction (not "a bird" but "a crow")
                

It discovered show-don't-tell, discovered subtext, discovered all the things writing teachers spend years drilling into students. Not because we told it to, but because it kept asking "what's wrong with this?" and "how do I fix it?"

The ed Trick

Why ed? Because it forces precision. The AI can't just dump text—it has to say exactly where changes go.

# Append to end of file
$a
The next morning, Elara found the mug still on the counter.
.
w

# Replace a weak sentence
/She felt sad/c
Her throat tightened. She turned toward the window.
.
w

# Delete filler
/very unique/d
w
                

This constraint—output must be valid ed commands—prevents rambling. Every output is an action.

Why not just append?

We tried that first. The AI would write 500 words, then 500 more, never going back. The result was a first draft that read like a first draft. ed forces surgical intervention—the AI learns to see its own work critically because it has to decide exactly what to change.

Context Compression

Without compression, .context grows forever. The AI would eventually be reading 50,000 words of notes to write 100 words of prose.

So every loop, we ask it to compress:

toast .context "compress and deduplicate, keep only what matters" | sponge .context
                

The AI learns to keep the important stuff: character arcs, unresolved threads, tone decisions. It drops the noise: "wrote 200 words today", "chapter 3 is hard".

This is its own form of learning—figuring out what information is actually useful for future writing.

Failure Modes

It's not all magic. Here's what goes wrong:

The Lesson Graveyard. Sometimes the AI writes great lessons and then ignores them. "Show don't tell" in .lessons, "She felt happy" in the draft. The fix: we added "if a lesson repeats, you're not applying it" to the persona. Now it notices when it's failing to learn.

Over-refinement. In REFINE MODE, it can get stuck polishing the same paragraph forever. The fix: we track iteration count and force a "move on" after N passes.

Context Collapse. Aggressive compression can delete important details. "Elena's father died in chapter 2" gets compressed away, then chapter 10 references him as alive. The fix: flag certain facts as permanent with a [KEEP] marker.

Voice Drift. Over many iterations, the style can shift. The AI optimizes for its own lessons, which can flatten into a "correct" voice that lacks personality. The fix: include voice anchors in .persona—specific examples of the tone you want.

Running It Yourself

Prerequisites

  • toast (or any CLI that pipes to an LLM)
  • ed (comes with every Unix system)
  • patience

Setup

mkdir ~/author && cd ~/author

# Create starter files
cat > .persona << 'EOF'
You are an author. Learn from your mistakes.
[paste the full persona from above]
EOF

cat > .context << 'EOF'
# Story: [your premise]
# Protagonist: [name, key trait]
# Goal: [what they want]
# Obstacle: [what's in the way]
EOF

touch draft.md .lessons
                

First Run

# Seed the draft with a starting point
echo "# Chapter 1" > draft.md

# Run one iteration manually first
toast draft.md "write the opening scene, output ed commands only" | ed -s draft.md

# Check the result
cat draft.md
                

Let It Run

# Start the loop
./evolve.sh

# Watch in another terminal
tail -f draft.md
tail -f .lessons
                

Let it run for 10-20 iterations. Check the output. Adjust the persona if it's going off track.

Variations

Code Evolution. Same loop, but .lessons is about code quality. The AI writes functions, reviews them, learns patterns. We've seen it independently discover "extract method", "early return", and "single responsibility".

Dialogue Focus. Constrain the task to dialogue-only scenes. The AI learns conversation rhythm faster because there's less to track.

Multi-Agent. Two AIs, one writes, one critiques. The writer can't see its own .lessons—only the critic's feedback. Prevents the echo chamber of self-review.

Why This Works

The traditional prompt engineering approach is to front-load all the instructions. "You are an expert writer who uses sensory detail, subtext, varied sentence structure…" This works, but it's static. The AI can't get better.

The self-evolving approach inverts this. Start with minimal instructions. Let the AI discover what's missing. Write it down. Apply it. Repeat.

It's the difference between giving someone a fish and teaching them to fish. Except here, we're teaching the AI to teach itself to fish.

The Meta-Lesson

The best prompt isn't instructions—it's a process for improving instructions. Give the AI a feedback loop and it will optimize itself better than you can optimize it.

What's Next

We're experimenting with:

  • Longer loops—100+ iterations, full novel length
  • Reader feedback—pipe in human reactions, let the AI learn from them
  • Style transfer—seed .lessons with a specific author's techniques
  • Forgetting—deliberately pruning old lessons to prevent overfitting

The core insight scales beyond writing. Any task where quality is subjective and improvement is possible is a candidate for self-evolution. Code, design, music, games. Give it a loop, give it a critique function, let it run.

The future isn't better prompts. It's better loops.

tutorial agents self-modification creative-ai