Explain Code Prompt — the Understanding Contract
"Explain this code" gets line-by-line narration. The understanding contract gets strategy, audience-fit, design decisions, and inference labeled as inference.
Explain the idea before the implementation: numbered steps with their contribution, honest best/worst-case complexity, and the alternatives each decision point rejected.
Algorithm explanations fail in two directions: pure narration (restating each line) or pure theory (lecturing about the general algorithm while ignoring this implementation). This prompt demands both halves connected: the approach and why it fits the problem, the logic in numbered steps with what each contributes, complexity analyzed honestly — time and space, best and worst case, and which input shapes trigger the worst — and the tradeoffs named: what this algorithm gives up for what it gains. One concrete input gets traced through the whole thing, state shown after each step. The loaded setup carries a real token-bucket rate limiter to break down.
Idea before implementation
What approach this is and why it fits the problem — before any line gets discussed.
Trace one concrete input
Real values walk the whole algorithm, with the state shown after each step.
Name the tradeoffs
Memory for speed, precision for simplicity — and at each decision point, the alternative that lost.
The ASSUMPTIONS section forces the split: anything verifiable from the source (the code DOES) stays factual, while any claim about intent, history, or rationale must be tagged as an INFERENCE unless a comment, test, or telling name evidences it. Where behavior can't be read from the token-bucket code, the output says 'cannot be determined from this code' instead of inventing a design story.
The EXECUTION FLOW section requires one concrete input walked through the whole algorithm with state shown after each step, so the refill math (elapsed x REFILL_RATE, capped at CAPACITY) and the tokens >= 1 branch get evaluated on actual values, not paraphrased. It also names which settings the flow depends on, so you can check the trace against your real config.
Strategy step 3 demands time and space for best AND worst case plus the input shapes that trigger the worst, because for this per-key bucket map the honest answer turns on distinct keys and eviction, not a single O(1) label. That worst-case-plus-triggers framing separates the breakdown from a textbook Big-O tag that would hide how the token-bucket map grows with traffic.
"Explain this code" gets line-by-line narration. The understanding contract gets strategy, audience-fit, design decisions, and inference labeled as inference.
For code whose authors are gone: reconstruct likely intent as labeled inference, hunt undocumented assumptions, and end with a maintenance brief — what's safe to touch.
Explain a system's shape, not its statements: layers and their owners, boundary contracts, dependency direction, and one request traced through every handoff.
"Review this code" gets shallow comments. The review contract gets findings with severities, a checklist, and a verdict.
"Refactor this code" invites silent behavior changes. The refactoring contract preserves business rules, outputs, and side effects — and flags uncertainty instead of deciding it.
"Fix this error" gets guesses. The investigation contract gets a ten-stage diagnosis: facts separated from assumptions, alternatives weighed, fixes justified.
Build code explanation prompts — mode, audience, and depth turn into an understanding contract that teaches, not narrates.