Home Knowledge Base Code Documentation with LLMs

Code Documentation with LLMs

Use Cases for LLM-Powered Documentation

1. Generate Docstrings Transform undocumented functions into fully documented ones:

**Before**
def process(data, threshold=0.5):
    return [x for x in data if x > threshold]

**After (LLM-generated)**
def process(data: list[float], threshold: float = 0.5) -> list[float]:
    """
    Filter numeric data by threshold.

    Args:
        data: List of numeric values to filter.
        threshold: Minimum value for inclusion (default: 0.5).

    Returns:
        List of values exceeding the threshold.

    Example:
        >>> process([0.1, 0.6, 0.3, 0.9], 0.5)
        [0.6, 0.9]
    """
    return [x for x in data if x > threshold]

2. Explain Complex Code Make legacy or unfamiliar code understandable:

Prompt: "Explain this code in plain English, then add inline comments"

Input: complex_algorithm.py
Output: Step-by-step explanation + commented version

3. Generate README Files Create comprehensive project documentation:

4. API Documentation Auto-generate OpenAPI specs and usage examples from code.

Prompting Techniques

Documentation Style Control

Add Google-style docstrings to all functions in this Python module.
Include:
- Brief description
- Args with types and descriptions
- Returns with type and description
- Raises for exceptions
- Example usage where helpful

Explanation Levels

LevelPrompt AdditionAudience
Beginner"Explain like I'm new to coding"Juniors
Standard"Explain what this code does"Developers
Expert"Analyze the algorithm complexity and design decisions"Seniors

Tools and Integrations

IDE Extensions

ToolIDEFeatures
GitHub CopilotVSCode, JetBrainsInline suggestions
CursorCursor IDEFull codebase context
CodeiumMultipleFree alternative
ContinueVSCodeOpen source

CLI Tools

**Generate docs for a file**
llm-docs generate --style google --file main.py

**Explain a function**
cat complex_function.py | llm "explain this code"

Best Practices

Do

Avoid

Example Workflow

import openai

def document_function(code: str, style: str = "google") -> str:
    """Generate documentation for a code snippet."""
    response = openai.chat.completions.create(
        model="gpt-4",
        messages=[{
            "role": "user",
            "content": f"Add {style}-style docstrings to this Python code:

{code}"
        }]
    )
    return response.choices[0].message.content
docdocumentationexplain codecomment

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.