Back to agents
Agent #4
š Documentation Generation Agent
This agent automatically generates comprehensive documentation from code, including API docs, README files, user guides, and code examples. It extracts information from code comments, type hints, and function signatures. python 04_doc_generation.py /path/to/api.py python 04_doc_generation.py "def calculate_tax(amount, rate)"
Key Features
- āAPI documentation
- āCode example generation
- āREADME creation
- āArchitecture diagrams
- āQuick start guides
Implementation
Download .py04_doc_generation.py
#!/usr/bin/env python3
"""
Documentation Generation Agent
This agent automatically generates comprehensive documentation from code,
including API docs, README files, user guides, and code examples.
It extracts information from code comments, type hints, and function signatures.
Usage:
python 04_doc_generation.py /path/to/api.py
python 04_doc_generation.py "def calculate_tax(amount, rate)"
"""
import sys
from anthropic import Anthropic
def main():
if len(sys.argv) < 2:
print("Usage: python 04_doc_generation.py <file_path_or_code>")
sys.exit(1)
target = " ".join(sys.argv[1:])
client = Anthropic()
system_prompt = """You are a documentation expert. Your job is to:
1. Analyze code to extract functionality and behavior
2. Generate clear, concise documentation
3. Create usage examples
4. Document parameters, return values, and exceptions
5. Provide architecture/design explanations
6. Generate multiple formats (Markdown, OpenAPI, etc.)
Write documentation that is beginner-friendly but technically accurate."""
task = f"""Generate comprehensive documentation for:
{target}
Provide:
1. Module/API overview
2. Function documentation with:
- Purpose
- Parameters and types
- Return values
- Exceptions
- Usage examples
3. Architecture diagram in text form
4. Quick start guide
5. Common patterns and best practices
6. FAQ or troubleshooting section
Format in clear Markdown."""
print("š Starting Documentation Generation Agent...\n")
print("=" * 60)
try:
with client.messages.stream(
model="claude-opus-4-7",
max_tokens=3000,
system=system_prompt,
messages=[{"role": "user", "content": task}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print("\n" + "=" * 60)
print("\nā
Documentation generation complete!")
except Exception as e:
print(f"\nā Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
How to Use
This agent is designed to be run from the command line:
python 04_doc_generation.py <input>The agent will stream its response to the console as it works through the task. Use Ctrl+C to interrupt at any time.
Quick Download
04_doc_generation.pyFile
04_doc_generation.py
Language
Python 3.8+
Dependencies
anthropic
Setup
- 1. Install:
pip install anthropic - 2. Download the .py file
- 3. Set ANTHROPIC_API_KEY
- 4. Run:
python 04_doc_generation.py