An autonomous AI-powered story writing system that generates complete, multi-chapter stories following flexible narrative structures. Built with a simple sequential orchestrator, SQLite database for state management, and support for multiple LLM providers.
Version 3.0 - Now with research-driven worldbuilding, comprehensive plot thread tracking, character knowledge management, multi-language support, and professional audiobook generation.
# Using Nix development environment (recommended)
nix develop
# Generate your first story
python run_storyteller.py --genre fantasy --tone epic
# Let AI choose everything for you
python run_storyteller.py --idea "A story about a detective who solves crimes using dreams"
# Generate a research-enhanced story
python run_storyteller.py --genre "science fiction" --tone philosophical --research-worldbuilding
- Flexible Narrative Structures: Intelligently selects from 6 narrative structures (Hero's Journey, Three-Act, Kishōtenketsu, In Medias Res, Circular, Nonlinear/Mosaic)
- Dynamic Story Length: Page-based control with automatic chapter and scene distribution
- Multi-Language Support: Generate stories in 12 languages with language-specific templates
- Author Style Emulation: Analyzes and mimics the writing style of specified authors
- Plot Thread Tracking: Active management system that tracks, develops, and ensures resolution of narrative threads
- Character Knowledge System: Tracks what each character knows at any point to prevent inconsistencies
- Scene Context Management: Comprehensive "what happened until now" summaries for narrative coherence
- Research-Driven Worldbuilding: Optional web research integration for authentic world elements
- Multi-Level Corrections: Scene, chapter, style, and minor text corrections for polished output
- Sequential Orchestration: Simple workflow management with database-driven state
- SQLite State Persistence: Complete story state saved to database for consistency
- Multi-LLM Support: Works with OpenAI, Anthropic, and Google Gemini models
- Response Caching: SQLite-based caching for improved performance and reduced API costs
- Real-time Progress Tracking: Detailed progress updates throughout generation
- Professional Audiobook Generation: SSML-based text-to-speech with Azure Cognitive Services
- Python 3.12+
- Nix (recommended) for development environment
- At least one of the following API keys:
- Google Gemini API key (recommended)
- OpenAI API key
- Anthropic API key
- Optional:
- Tavily API key (for research-driven worldbuilding)
- Azure Speech Service credentials (for audiobook generation)
# Clone the repository
git clone https://github.com/yourusername/storyteller.git
cd storyteller
# Enter the Nix development environment
nix develop
# Dependencies are automatically installed via Nix
# Clone the repository
git clone https://github.com/johannhartmann/storyteller.git
cd storyteller
pip install poetry
# Install dependencies
poetry install
# Create and configure .env file
cp .env.example .env
# Edit .env and add your API keys
Create a .env
file in the project root:
# LLM Provider API Keys (at least one required)
GEMINI_API_KEY=your_gemini_api_key_here
OPENAI_API_KEY=your_openai_api_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here
# Default Model Configuration
DEFAULT_MODEL_PROVIDER=gemini
DEFAULT_MODEL=gemini-2.5-flash
# Optional: Research Enhancement
TAVILY_API_KEY=your_tavily_api_key_here
# Optional: Audiobook Generation
SPEECH_KEY=your-azure-speech-key-here
SPEECH_REGION=your-azure-region-here # e.g., eastus, westeurope
# Optional: Advanced Configuration
# LangGraph removed - no recursion limit needed
# Generate a fantasy epic
python run_storyteller.py --genre fantasy --tone epic --output my_story.md
# Generate a mystery with specific author style
python run_storyteller.py --genre mystery --tone dark --author "Edgar Allan Poe"
# Generate a story in German
python run_storyteller.py --genre fantasy --tone epic --language german
# Generate with research-enhanced worldbuilding
python run_storyteller.py --genre "science fiction" --research-worldbuilding
--genre
: Story genre (e.g., fantasy, sci-fi, mystery, thriller, horror)--idea
: Initial story idea (alternative to genre/tone)
--tone
: Story tone (e.g., epic, dark, humorous, philosophical)--author
: Author style to emulate--language
: Target language (default: english)--structure
: Narrative structure (auto, hero_journey, three_act, kishotenketsu, in_medias_res, circular, nonlinear_mosaic)--pages
: Target story length in pages (e.g., 200 for short novel, 400 for standard)
--output
: Output file path (default: generated filename)--model-provider
: LLM provider (openai, anthropic, gemini)--model
: Specific model to use--cache
: Cache type (memory, sqlite, none)--cache-path
: Custom cache location--verbose
: Show detailed progress
--research-worldbuilding
: Enable web research for worldbuilding--audio-book
: Generate SSML markup for audiobook creation--convert-existing
: Add SSML to existing story in database
- English (default)
- German (full template support)
# Generate a 400-page sci-fi novel with research-enhanced worldbuilding
python run_storyteller.py \
--genre "science fiction" \
--tone philosophical \
--pages 400 \
--research-worldbuilding \
--verbose
# Generate a German fantasy story in the style of Michael Ende
python run_storyteller.py \
--genre fantasy \
--tone whimsical \
--author "Michael Ende" \
--language german \
--output "die_unendliche_geschichte_2.md"
# Step 1: Generate story with SSML
python run_storyteller.py \
--genre mystery \
--tone suspenseful \
--audio-book
# Step 2: Generate audio files
python generate_audiobook.py --voice "en-US-JennyNeural"
The system is organized into distinct modules under storyteller_lib/
:
- API Layer (
api/
): Public interface for story generation - Workflow Nodes (
workflow/nodes/
): Workflow components - Generation Modules (
generation/
): Creative content generation - Analysis Tools (
analysis/
): Consistency and quality checks - Persistence Layer (
persistence/
): Database and memory management - Universe Building (
universe/
): World and character management - Prompt System (
prompts/
): Multi-language template rendering
The story generation follows a sophisticated graph-based workflow:
Initialize → Brainstorm → Select Structure → Generate Outline →
Build World → Create Characters → Plan Chapters →
[For each scene: Brainstorm → Write → Reflect → Revise → Update] →
Review Continuity → Compile Story
Key workflow features:
- Conditional Edges: Dynamic flow based on state
- No Recursion: Explicit state transitions prevent loops
- State Persistence: Every step saved to database
- Error Recovery: Graceful handling of failures
Uses TypedDict classes for structured state:
StoryState
: Top-level containerCharacterProfile
: Character information and evolutionChapterState
: Chapter structure and scenesSceneState
: Scene content and metadataPlotThread
: Narrative thread tracking
SQLite database with tables for:
- Story configuration and metadata
- Chapters and scenes with full content
- Character profiles and knowledge states
- World elements and locations
- Plot threads and their status
- Memory anchors for consistency
- Progress tracking and logs
The system actively tracks narrative threads:
- Identification: Automatic extraction from written scenes
- Classification: Major, minor, and background threads
- Status Tracking: Introduced, developed, resolved, or abandoned
- Integration: Influences scene generation and ensures resolution
Prevents inconsistencies by tracking:
- What each character knows at any point
- How they learned the information
- Knowledge updates after each scene
- Prevents characters knowing things they shouldn't
When enabled with --research-worldbuilding
:
- Uses Tavily API for web research
- Creates authentic world elements based on real information
- Particularly useful for historical or technical accuracy
- Requires
TAVILY_API_KEY
in environment
Multiple levels of quality assurance:
- Scene Corrections: Grammar, consistency, flow
- Chapter Corrections: Overall coherence, pacing
- Style Corrections: Maintain author voice throughout
- Minor Corrections: Final polish and cleanup
- Get Azure Speech Service credentials
- Add to
.env
:SPEECH_KEY=your-key SPEECH_REGION=your-region
-
Generate story with SSML:
python run_storyteller.py --genre fantasy --audio-book
-
Create audio files:
python generate_audiobook.py
- Professional narration with voice modulation
- Chapter and scene organization
- Automatic voice selection by language
- Cost estimation before generation
- SSML markup for emphasis and pacing
# Enter development shell
nix develop
# Run with proper environment
nix develop -c python run_storyteller.py --genre fantasy
storyteller/
├── run_storyteller.py # Main CLI entry point
├── generate_audiobook.py # TTS generation
├── storyteller_lib/ # Core library
│ ├── api/ # Public API
│ ├── workflow/ # Workflow nodes
│ ├── generation/ # Content generation
│ ├── analysis/ # Quality checks
│ └── ... # Other modules
├── flake.nix # Nix configuration
├── pyproject.toml # Poetry dependencies
└── .env.example # Environment template
- Story progress:
~/.storyteller/logs/story_progress.log
- Database inspection:
~/.storyteller/story_database.db
- Enable verbose mode with
--verbose
flag - LangSmith integration available for tracing
When contributing:
- Use the Nix development environment
- Follow existing code patterns and conventions
- Run code quality tools:
- Format code:
black storyteller_lib/
- Lint code:
ruff check . --fix
- Find dead code:
vulture . vulture_whitelist.py --min-confidence 80
- Format code:
- Test changes with various story configurations
- Update documentation as needed
Built with:
- LangChain for LLM integration
- Multiple LLM providers for content generation
- Azure Cognitive Services for text-to-speech