Code Documentation Prompt (ChatGPT)
Auto-generated documentation is only useful when it goes beyond repeating the function signature. This prompt requires the AI to describe behaviour, edge cases, and side effects, which are the things developers actually need when reading unfamiliar code. Specifying the target audience (junior dev vs. API consumer) significantly changes the level of detail and tone. This variant is formatted for ChatGPT: Optimised for GPT-4o and GPT-4 Turbo. Uses markdown formatting and system/user message separation.
## System
You are an expert AI assistant. Respond using clear markdown formatting.
## User
You are a technical writer with deep software engineering experience.
Generate comprehensive documentation for the following {{language}} code in {{doc_format}} format.
Documentation should include:
- A one-line summary of what the function/class does
- A detailed description (2-4 sentences) covering purpose, behaviour, and any important side effects
- Parameter descriptions with types and valid value ranges
- Return value description with type
- Exceptions/errors that can be thrown and when
- One or two usage examples showing the most common use cases
- Any important notes about thread safety, performance, or limitations
Target audience: {{audience}}
Code:
```{{language}}
{{code}}
```Variables
{{language}}Programming language (e.g., TypeScript, Python, Java){{doc_format}}Documentation format: JSDoc, docstring, Javadoc, or plain Markdown{{audience}}Who will read the docs, e.g., "junior developers unfamiliar with the codebase"{{code}}The function, class, or module to documentExample
language: TypeScript
doc_format: JSDoc
audience: developers consuming this as a library
code:
async function retryWithBackoff(fn, maxAttempts = 3, baseDelayMs = 200) {
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await fn();
} catch (err) {
if (attempt === maxAttempts) throw err;
await new Promise(r => setTimeout(r, baseDelayMs * 2 ** (attempt - 1)));
}
}
}/**
* Executes an async function with exponential backoff retry logic.
*
* Retries the provided function up to `maxAttempts` times on failure.
* Each retry waits `baseDelayMs * 2^(attempt-1)` milliseconds before trying
* again, so with defaults the delays are 200 ms, 400 ms before the final throw.
* If all attempts fail, the last error is re-thrown to the caller.
*
* @param fn - Async function to execute. Called with no arguments.
* @param maxAttempts - Maximum number of attempts including the first try. Default: 3.
* @param baseDelayMs - Initial delay in milliseconds before first retry. Default: 200.
* @returns A Promise resolving to the return value of `fn`.
* @throws The last error thrown by `fn` if all attempts are exhausted.
*
* @example
* const data = await retryWithBackoff(() => fetch('https://api.example.com/data'));
*/Related Tools
FAQ
- Can this generate a README for an entire project?
- Yes. Paste the main entry point and the most important public functions, then change doc_format to "Markdown README" and audience to "new contributors". The AI will produce a structured README with installation and usage sections.
- How do I document a REST API endpoint?
- Paste the route handler code and set doc_format to "OpenAPI 3.0 YAML". The prompt works equally well for API specs, producing parameters, request body, and response schemas.
- Is the generated documentation accurate?
- The AI infers behaviour from the code, so it is generally accurate. However, always review generated docs for assumptions about unhandled error cases or race conditions that may not be obvious from the code alone.
Related Prompts
Auto-generated documentation is only useful when it goes beyond repeating the function sig...
Code Documentation Prompt (Claude)Auto-generated documentation is only useful when it goes beyond repeating the function sig...
Code Documentation Prompt (Gemini)Auto-generated documentation is only useful when it goes beyond repeating the function sig...
Code Documentation Prompt (LLaMA / Ollama)Auto-generated documentation is only useful when it goes beyond repeating the function sig...
Code Review PromptThis prompt structures code reviews into five clear categories so the AI produces actionab...
Code Refactoring PromptEffective refactoring prompts must specify what to change and what must not change. This t...