The Error Code Documentation Disasters We've All Seen — and How to Fix Them with an AI Pipeline
1. The Documentation Disasters We've All Seen
If you've ever maintained a decent API service, these scenarios probably look familiar:
Disaster one: code first, documentation last. The API is live, error codes are scattered across catch blocks and enum classes, and the docs only say "400 means bad request." A user asks "what's the difference between 40001 and 40002," and you spend ten minutes searching your own codebase before daring to reply.
Disaster two: one-shot AI generation, full of hallucinations. You've heard AI can write docs, so you throw your entire service description at a model and say "generate API documentation for me." The output looks beautiful — complete parameters, proper formatting — but on closer inspection: user_id became userId, a nonexistent error code 50099 sits in the middle of the table, and the fields in the response example don't match the actual return values. Users integrate based on the docs and crash immediately.
Disaster three: docs written, never updated. The last iteration changed the semantics of some error codes, but the docs are still three months old. A user spends half a day debugging against stale documentation, then posts in the group chat: "your docs don't match your actual responses," and your face burns a little.
The common thread in all these problems: treating AI as a generator that replaces thinking, rather than a constrained processing pipeline. Here's how to get this right.
2. Where Users' Real Pain Points Are
Independent developers and small teams face very concrete pain points with API documentation:
- No time. Writing docs looks like a "non-value-producing" cost, so it always gets deprioritized behind development — and ends up never getting done.
- Scattered sources of truth. Error codes are defined in code, in comments, in verbal agreements, or worse, only in someone's head.
- Inconsistent formatting. Today's docs differ from last month's in field ordering, naming style, and error code grouping logic, making them hard to read.
- Updates fall behind. Once documentation drifts from code, it's worse than no documentation at all — it actively misleads users.
AI happens to be great at exactly this kind of "mechanical but tedious" work: given structured input (code, comments, error code enums), its output speed and consistency far exceed manual curation. The catch: you have to feed it the right things.
3. The Correct Workflow
Step 1: Consolidate your information sources before generating anything.
Don't expect AI to understand your service out of thin air. First, gather the actual error code enums, the API request/response structs, and key comments. Even just three pasted code snippets beat a description like "my service is a user management service" by a factor of ten. AI doesn't create information; it organizes it — that's the first gate against hallucination.
Step 2: Use constrained prompts, not just "write docs for me."
Tell the AI explicitly: use only the error codes I provide; do not add new ones or guess; the output format is fixed; every error code must include trigger scenarios and handling suggestions. You can require it in the prompt to "mark missing information as TODO instead of filling it in" — this alone drives hallucination rates way down.
Step 3: Spot-check manually instead of proofreading everything.
You don't need to review the generated docs word by word. Spot-check three high-risk areas: whether the error code count matches the enum, whether parameter types match the structs, and whether the response examples can be reproduced with a local request. If the spot-checks pass, you're generally safe to publish.
Step 4: Make the process repeatable.
Save your prompt as a template and re-run it after every iteration. Documentation updates go from "whenever we remember" to "run it right after changing the code" — this is the most critical process shift.
Here's a ready-to-copy prompt template:
你是一位资深 API 文档工程师。请根据我提供的代码材料,生成 API 文档和错误码说明。
【硬性规则】
1. 只能使用我提供的错误码、字段名、参数,严禁新增、改名或猜测任何内容。
2. 如果信息缺失,在对应位置写【TODO:待补充】,不要自行补全。
3. 错误码总数必须与我提供的枚举数量一致,输出前先数一遍。
【输出格式】
## 一、接口说明
- 接口名称 / 请求方法 / 路径
- 请求参数表格(参数名、类型、必填、说明)
- 响应示例(基于我提供的结构体生成 JSON)
## 二、错误码说明
表格包含五列:错误码、HTTP 状态码、含义、常见触发场景、用户处理建议
## 三、接入注意事项
列出 2-4 条我从注释中提取的真实注意点,没有则写「无」
【我的代码材料如下】
(在此粘贴错误码枚举、接口结构体、相关注释)4. Before and After AI
| Dimension | Before (manual grind) | After (process + AI) |
|---|---|---|
| First draft | Days of procrastination, or never | Initial draft in minutes once materials are ready |
| Format consistency | Style drifts over time | Fixed template, stable style |
| Error code docs | Just a "meaning" column | Includes trigger scenarios + handling suggestions |
| Iterative updates | Code changes, docs don't | Re-run the template in minutes to sync |
| User questions | Same questions answered repeatedly in chat | Just say "see section 3 of the docs" |
Note that the most valuable part of this comparison isn't "speed" — it's consistency and maintainability. Documentation goes from one-off labor to a repeatable process, and that's what small teams actually need.
5. Pitfalls That Are Easy to Fall Back Into
- Don't let AI "incidentally" design your error code system. Error code grouping and numbering rules should be defined by you in the code. AI explains; it doesn't invent.
- Don't use generated docs to modify code in reverse. If you find a conflict between docs and code, fix the code materials and regenerate. Don't let the docs become a second source of truth.
- Different models do perform differently. If a particular model hallucinates frequently, switch models or tighten your prompt constraints. For tool selection, evaluate each vendor's pricing page and free tiers yourself.
Final Thoughts
API documentation and error code references are, by nature, work where "the cleaner the input, the more reliable the output." Treat AI as a pipeline, not a wishing well, and it becomes the most cost-effective documentation colleague a small team can have.
If you're looking for a stable, transparently priced model API to run this kind of documentation workflow, give ThisToken a try — it aggregates major models behind a single key. Register here: https://api.thistoken.ai/register
---
Tired of juggling provider integrations? Register at https://api.thistoken.ai/register and call every model through one base_url.
Bạn muốn thử Token.AI?
Tạo API Key cấp dự án, bật kênh trong bảng điều khiển và định cấu hình định tuyến, ngân sách và nhật ký kiểm tra.
注册 ThisToken.AI 并获取 API Key