An autonomous agent powered by Z.AI's GLM-4.7 model (with the Coding Plan) for creating novels, books, and short story collections.
- π€ Autonomous Writing: The agent plans and executes creative writing tasks independently
- π Multiple Formats: Create novels, books, or short story collections
- πΎ Smart Context Management: Automatically compresses context when approaching token limits
- π Recovery Mode: Resume interrupted work from saved context summaries
- π Token Monitoring: Real-time tracking of token usage with automatic optimization
- π οΈ Tool Use: Agent can create projects, write files, and manage its workspace
- π§ Advanced Reasoning: Uses GLM-4.7's powerful reasoning capabilities
We recommend using uv for fast Python package management:
# Install uv (if you don't have it)
curl -LsSf https://astral.sh/uv/install.sh | sh- Install dependencies:
Using uv (recommended):
uv pip install -r requirements.txtOr using pip:
pip install -r requirements.txt- Configure your API key:
Create a .env file with your Z.AI API key:
# Create .env file with your API key
# The file should contain:
ZAI_API_KEY=your-api-key-hereGet your Z.AI API key from: https://open.bigmodel.cn/usercenter/apikeys
Run with an inline prompt:
# Using uv (recommended)
uv run writer.py "Create a collection of 5 sci-fi short stories about AI"
# Or using python directly
python writer.py "Create a collection of 5 sci-fi short stories about AI"Or run interactively:
uv run writer.py
# or: python writer.pyThen enter your prompt when asked.
If the agent is interrupted or you want to continue previous work:
uv run writer.py --recover output/my_project/.context_summary_20250107_143022.md
# or: python writer.py --recover output/my_project/.context_summary_20250107_143022.mdThe agent has access to three tools:
- create_project: Creates a project folder to organize the writing
- write_file: Writes markdown files with three modes:
create: Creates a new file (fails if exists)append: Adds content to an existing fileoverwrite: Replaces the entire file content
- compress_context: Automatically triggered to manage context size
- The agent receives your prompt
- It reasons about the task using GLM-4.7's capabilities
- It decides which tools to call and executes them
- It reviews the results and continues until the task is complete
- Maximum 300 iterations with automatic context compression
- Token Limit: 200,000 tokens (GLM-4.7's context window)
- Auto-Compression: Triggers at 180,000 tokens (90% of limit)
- Backups: Automatic context summaries every 50 iterations
- Recovery: All summaries saved with timestamps for resumption
gemini-writer/
βββ writer.py # Main agent
βββ tools/
β βββ __init__.py # Tool registry
β βββ writer.py # File writing tool
β βββ project.py # Project management tool
β βββ compression.py # Context compression tool
βββ utils.py # Utilities (token counting, etc.)
βββ requirements.txt # Python dependencies
βββ .env # API key configuration (create this)
βββ .gitignore # Git ignore rules
βββ README.md # This file
# Generated during use:
output/ # All AI-generated projects go here
βββ your_project_name/ # Created by the agent
β βββ chapter_01.md # Written by the agent
β βββ chapter_02.md
β βββ .context_summary_*.md # Auto-saved context summaries
βββ another_project/
βββ ...
uv run writer.py "Write a mystery novel set in Victorian London with 10 chapters"uv run writer.py "Create 7 interconnected sci-fi short stories exploring the theme of memory"uv run writer.py "Write a comprehensive guide to Python programming with 15 chapters"The agent displays its progress: Iteration X/300
Real-time token usage: Current tokens: 45,234/128,000 (35.3%)
Press Ctrl+C to interrupt. The agent will save the current context for recovery.
-
Be Specific: Clear prompts get better results
- Good: "Create a 5-chapter romance novel set in modern Tokyo"
- Less good: "Write something interesting"
-
Let It Work: The agent works autonomously - it will plan and execute the full task
-
Recovery is Easy: If interrupted, just use the
--recoverflag with the latest context summary -
Check Progress: Generated files appear in real-time in the project folder
Make sure you have created a .env file in the project root with your API key:
ZAI_API_KEY=your-actual-api-key-here- Verify your API key is correct in the
.envfile - Get your API key from: https://open.bigmodel.cn/usercenter/apikeys
Check write permissions in the current directory
The agent can run up to 300 iterations. For very complex tasks, this is normal. Check the project folder to see progress.
The agent automatically compresses context at 180K tokens. If you see compression messages, the system is working correctly.
- Model: glm-4.7 (via OpenAI-compatible API)
- API Endpoint: https://api.z.ai/api/coding/paas/v4 (Coding Plan)
- Temperature: 1.0
- Context Window: 200,000 tokens
- Max Iterations: 300
- Compression Threshold: 180,000 tokens
You can customize these settings in writer.py.
MIT License with Attribution Requirement - see LICENSE file for details.
Commercial Use: If you use this software in a commercial product, you must provide clear attribution to Pietro Schirano (@Doriandarko).
API Usage: This project uses the Z.AI GLM API. Please refer to Z.AI's terms of service for API usage guidelines.
- Created by: Pietro Schirano (@Doriandarko)
- Powered by: Z.AI's GLM-4.7 model (with Coding Plan)
- Repository: https://github.com/Doriandarko/gemini-writer