Generate novel-length Markdown books using Moonshot AI's Kimi K2 reasoning models (256k context, multi-step reasoning).
✨ New: Web UI for generating, managing, and reading novels! 🚀 One-click launch in GitHub Codespaces!
Two ways to use:
- 🖥️ Web UI - Streamlit-based interface for easy novel generation and management
- 💻 CLI - Command-line interface for automated workflows
API essentials straight from the docs on this page:
- Base URL:
https://api.moonshot.ai/v1 - Install OpenAI SDK
>=1.x - Recommended K2 models:
kimi-k2-thinking,kimi-k2-thinking-turbo(long-thinking, 256k; turbo is faster). - K2 is text-only; use Kimi Latest for images.
The easiest way to get started is using GitHub Codespaces:
-
Fork this repository first (click the Fork button at the top right)
-
In your fork, click the Code button → Codespaces → Create codespace on
main -
Wait for the environment to set up (automatic)
-
Create a
.envfile with your Moonshot API key:cp .env.example .env # Edit .env and add: MOONSHOT_API_KEY=sk-... -
Start the web UI:
# Option 1: Direct launch streamlit run app.py # Option 2: Using helper script (auto-installs dependencies) bash run-ui.sh
-
Click the notification to open the UI (or go to the Ports tab and open port 8501)
Why fork first? Forking gives you your own copy of the repository where you can save your generated novels. The "Publish" feature commits novels to your fork, preserving your work.
Web UI Features:
- 📝 Generate - Create new novels with a user-friendly form
- 📚 Library - Manage preview and published novels
- 📖 Reader - Read novels with chapter navigation
- ✅ Publish - Move novels from preview to published (auto-commits to repo)
- ⬇️ Download - Export novels as Markdown files
Directory Structure:
preview/- Novels being worked on (gitignored)published/- Finalized novels (committed to repo)
If you prefer the command-line interface:
# Using pip
pip install -e .
# Or using uv (recommended)
uv sync
# Configure
cp .env.example .env
# Edit .env and paste your Moonshot key:
# MOONSHOT_API_KEY=sk-...Optional .env overrides:
KIMI_MODEL=kimi-k2-thinking-turbo
KIMI_TEMPERATURE=0.6
KIMI_MAX_OUTPUT_TOKENS=4096Interactive:
python kimi_writer.py
# Or with uv: uv run kimi_writer.pyNon-interactive:
python kimi_writer.py --prompt "A near-future techno-thriller about..." --title "Ghosts in the Wire" --out book.mdUseful flags:
--resumeContinue fromnovel_state.json--chapters NLimit number of chapters to write:- Without
--resume: Write up to N chapters total (e.g.,--chapters 3writes chapters 1-3) - With
--resume: Write N more chapters from current progress (e.g., if 5 chapters exist,--resume --chapters 3writes chapters 6-8)
- Without
Artifacts:
book.md- full Markdown (title, outline, chapters)novel_state.json- checkpoint/resume state
- Outline phase. Asks K2 to create a 20-40 chapter outline as a numbered Markdown list.
- Chapter phase. Iterates over chapter titles, requesting ~1.5-2.5k-word chapters. A short rolling context (last few chapter snippets) is sent to preserve continuity without exhausting context.
- Streaming + retries. Output streams to console (dots), with exponential backoff for robustness.
-
Generate Tab
- Enter novel concept, title, and settings
- Watch real-time generation progress
- Novels saved to
preview/directory (gitignored) - Resume incomplete novels anytime
-
Library Tab
- Browse preview and published novels
- Read novels with chapter navigation
- Download as Markdown files
- Publish to move from preview → published (commits to repo)
- Delete unwanted novels
-
Reader Mode
- Clean reading interface
- Chapter-by-chapter navigation
- Full markdown rendering
From the quickstart:
- 256K context for long projects.
- Multi-step reasoning/tool use for complex tasks.
- Turbo variants can reach 60-100 tok/s.
kimi-book-writer/
├── app.py # Streamlit web UI
├── kimi_writer.py # CLI novel generator
├── utils.py # Helper utilities
├── preview/ # Draft novels (gitignored)
├── published/ # Published novels (committed)
├── examples/ # Example generated novels
├── .devcontainer/ # Codespaces configuration
├── .env.example # Environment template
└── pyproject.toml # Dependencies
- Both the CLI and Web UI share the same core generation logic
- Prompts in
kimi_writer.pycan be customized for different genres/styles - The Web UI provides better progress tracking and novel management
- CLI is useful for automation and scripting workflows
MIT