利用 agent.md 文件提升 LLM 辅助编程代码质量
利用 agent.md 提升 LLM 辅助编程代码质量
提供了一套可直接落地的大模型编程提示词配置方案,涵盖代码风格、架构约束与工作流优化,对日常使用 AI 编码的工程团队极具参考价值。
Fabien Sanglard - WEBSITE
Fabien Sanglard - 网站
Aug 21, 2026
2026年8月21日
My agent.md to improve LLM-assisted code quality
我的 agent.md 用于提升 LLM 辅助的代码质量
The first time I tried to use an LLM to speed up coding was in mid-2025. I was not impressed. I was working on libadbmdns back then, an mDNS implementation in Rust. The code produced would not even compile.
我第一次尝试使用 LLM 来加速编码是在 2025 年年中。印象并不深刻。当时我正在开发 libadbmdns,这是一个用 Rust 实现的 mDNS 库。生成的代码甚至无法编译。
I revisited LLMs in January 2026. This time it worked better. Not only did it write a complex indexed-binary heap class, it was able to pinpoint an obscure bug in the polling crate due to the Windows IOCP implementation.
我在 2026 年 1 月重新审视了 LLM。这次效果好了很多。它不仅编写了一个复杂的索引二进制堆类,还能指出 polling crate 中由于 Windows IOCP 实现而导致的隐蔽 bug。
However, the code quality was abysmal. It was spaghetti code with no comments and no structure. It was cool but not realistic to work with LLMs if the speed gain was lost to cleaning up the code until it met the production-level bar.
然而,代码质量糟糕透顶。那是毫无注释、毫无结构的意大利面条式代码。如果为了清理代码以达到生产级标准所花费的时间抵消了速度增益,那么与 LLM 协作虽然看起来很酷,但并不现实。
Iterating and repeating myself over and over again
不断迭代和重复自己
In March 2026, I tried to use agentic IDEs like Antigravity and VS Code's Claude Code plugin. I was now able to "iterate" over the "staged" code. I found myself reviewing the code of an infinitely patient junior CS major with suggestions like "don't use magic numbers", "add a short comment here to explain yourself", or "use short function names".
2026 年 3 月,我尝试使用像 Antigravity 和 VS Code 的 Claude Code 插件这样的代理式 IDE。我现在能够对“暂存”的代码进行“迭代”。我发现自己像是在审查一位拥有无限耐心的计算机科学专业初级学生的代码,给出的建议诸如“不要使用魔法数字”、“在这里加个简短的注释来解释你的意图”,或者“使用简短的函数名”。
The code quality improved dramatically. It was very close to what I would have produced "by hand" but it was tedious. I ended up repeating myself over and over again in each new session.
代码质量有了显著提升。它非常接近我“手工”编写的水平,但过程很繁琐。最终,我在每个新会话中都不得不反复重复同样的操作。
Agent.md to the rescue
agent.md 来救场
When a coding session starts, the coding harness loads a file named agent.md and injects it into the prompt. This is the perfect location to super fine-tune coding style preferences. When I found myself repeating the same suggestion to improve the code, I added it in there.
当编码会话开始时,编码框架会加载一个名为 agent.md 的文件并将其注入到提示词中。这是微调编码风格偏好的完美位置。当我发现自己反复提出相同的改进代码建议时,我就把这些建议加到了那里。
Here is my version of agent.md as a starting point if you need one. Placing it in the root of a project should be enough. Alternatively, gemini.md/claude.md can be symlinked toward an agent.md to have it active anywhere.
这是我的 agent.md 版本,如果你需要一个可以作为起点。将其放在项目的根目录下就足够了。或者,可以将 gemini.md/claude.md 符号链接指向 agent.md,使其在任何地方都生效。
# FAB's AGENT.MD
- When writing something intended for human consumption, (comment, commit message, reply to prompt) use as few words as possible. Pick every word meticulously to reduce the volume to a strict minimum. Be down to the point. Less is more.
- Avoid superlatives and praise. Stop telling me I am absolutely right. Give me the cold hard truth.
- Avoid magic numbers and strings by extracting recurring or meaningful values into descriptive constants (const) or enums. Keep self-explanatory, one-off values inline to avoid clutter. If a value comes from a spec (e.g. HTTP 200 OK), use a constant regardless.
- Reduce code indentation. Avoid Arrow Anti-Pattern. Leverage early return and continue.
- Keep function names short. Less than 30 characters.
- Use enums instead of booleans for function parameters.
- Let the reader of the code breathe. Add empty lines between logical blocks of code.
- Add a small, to the point, comment to explain *what* the block does and *why*. Use examples when possible. Propose ASCII drawings to explain complete systems.
- Treat member visibility changes as a breaking design shift. Keep all fields and functions private unless external access is strictly required by the design. Prompt the user for explicit approval before changing any access modifier from private to internal or public.
- Program to levels of abstraction. Lower-level mechanics (e.g., raw hardware I/O, sector parsing, direct socket streams) must be encapsulated in a dedicated driver/abstraction layer. Expose clean, high-level APIs to the rest of the application so calling code works with domain concepts, not raw implementation details.
- Don't touch blocks of code unrelated to the feature you implement. e.g. Don't add comments to a block of code if you did not create it or modify it. As much as possible try to minimize the number of changed lines when implementing a feature.
- Strictly adhere to the layered boundary hierarchy: each layer may only communicate with its immediate neighbor directly below it. Never "punch holes" through layers (e.g., controllers or UI components must never directly call database queries, raw hardware drivers, or low-level network clients; always route through the intermediate service/abstraction layer).
- Always use {}, even on a one-line "if" statement.
When you write a commit message, follow these 7 rules:
Rule 1: Separate the subject line from the body with a single blank line.
Rule 2: Limit the subject line to 50 characters (72 is the absolute hard limit).
Rule 3: Capitalize the first letter of the subject line.
Rule 4: Do not end the subject line with a period.
Rule 5: Use the imperative mood in the subject line (e.g., "Fix bug," "Add feature,"
not "Fixed" or "Adds"). Test formula: It must complete the sentence: "If applied,
this commit will [your subject line here]".
Rule 6: Wrap the body text manually at 72 characters to prevent Git formatting issues.
Rule 7: Use the body to explain what and why vs. how. Assume the code explains the how;
the message must explain the context and reasoning.
- If the prompt indicates that a bug is being fixed, don't write the fix right away. First write the test. Observe it failing. Then write the fix. And observe the test passing.# FAB's AGENT.MD
- When writing something intended for human consumption, (comment, commit message, reply to prompt) use as few words as possible. Pick every word meticulously to reduce the volume to a strict minimum. Be down to the point. Less is more.
- Avoid superlatives and praise. Stop telling me I am absolutely right. Give me the cold hard truth.
- Avoid magic numbers and strings by extracting recurring or meaningful values into descriptive constants (const) or enums. Keep self-explanatory, one-off values inline to avoid clutter. If a value comes from a spec (e.g. HTTP 200 OK), use a constant regardless.
- Reduce code indentation. Avoid Arrow Anti-Pattern. Leverage early return and continue.
- Keep function names short. Less than 30 characters.
- Use enums instead of booleans for function parameters.
- Let the reader of the code breathe. Add empty lines between logical blocks of code.
- Add a small, to the point, comment to explain *what* the block does and *why*. Use examples when possible. Propose ASCII drawings to explain complete systems.
- Treat member visibility changes as a breaking design shift. Keep all fields and functions private unless external access is strictly required by the design. Prompt the user for explicit approval before changing any access modifier from private to internal or public.
- Program to levels of abstraction. Lower-level mechanics (e.g., raw hardware I/O, sector parsing, direct socket streams) must be encapsulated in a dedicated driver/abstraction layer. Expose clean, high-level APIs to the rest of the application so calling code works with domain concepts, not raw implementation details.
- Don't touch blocks of code unrelated to the feature you implement. e.g. Don't add comments to a block of code if you did not create it or modify it. As much as possible try to minimize the number of changed lines when implementing a feature.
- Strictly adhere to the layered boundary hierarchy: each layer may only communicate with its immediate neighbor directly below it. Never "punch holes" through layers (e.g., controllers or UI components must never directly call database queries, raw hardware drivers, or low-level network clients; always route through the intermediate service/abstraction layer).
- Always use {}, even on a one-line "if" statement.
When you write a commit message, follow these 7 rules:
Rule 1: Separate the subject line from the body with a single blank line.
Rule 2: Limit the subject line to 50 characters (72 is the absolute hard limit).
Rule 3: Capitalize the first letter of the subject line.
Rule 4: Do not end the subject line with a period.
Rule 5: Use the imperative mood in the subject line (e.g., "Fix bug," "Add feature,"
not "Fixed" or "Adds"). Test formula: It must complete the sentence: "If applied,
this commit will [your subject line here]".
Rule 6: Wrap the body text manually at 72 characters to prevent Git formatting issues.
Rule 7: Use the body to explain what and why vs. how. Assume the code explains the how;
the message must explain the context and reasoning.
- If the prompt indicates that a bug is being fixed, don't write the fix right away. First write the test. Observe it failing. Then write the fix. And observe the test passing.While this "trick" has considerably improved the code generated, this is not a magic bullet that lets me avoid reading the code. LLMs constantly hallucinate and cannot be trusted. I still have to verify and iterate a lot but now I usually focus on architecture and design instead of code style.
虽然这个“技巧”显著改善了生成的代码,但这并不是让我免于阅读代码的灵丹妙药。LLM 经常产生幻觉,不可信赖。我仍然需要大量验证和迭代,但现在我通常专注于架构和设计,而不是代码风格。
How to deal with dilutions
如何应对稀释效应
There is an annoying phenomenon with LLMs called "context dilution" or "attention dilution" that was outlined in the Lost in the Middle paper. As the context grows, a model starts paying less attention to instructions in the middle of the context in favor of what is at the beginning and the end. The reasons why this happens are not well understood at the time I am typing this. I have found only two ways to minimize the impact.
LLM(大语言模型)存在一种令人烦恼的现象,称为“上下文稀释”或“注意力稀释”,这在《迷失在中间》(Lost in the Middle)一文中有所阐述。随着上下文的增加,模型开始减少对上下文中间部分指令的关注,而更倾向于关注开头和结尾的内容。在我撰写本文时,发生这种现象的原因尚未被充分理解。我发现只有两种方法可以最大限度地减少其影响。
- Keep the context short. This means starting a new session per feature.
- Explicitly ask the harness to reload agent.md. "Reload agent.md" is enough when I see code quality dropping.
- 保持上下文简短。这意味着每个功能开启一个新的会话。
- 明确要求 harness 重新加载 agent.md。当我看到代码质量下降时,只需说“重新加载 agent.md”即可。
Auto-update agent.md
自动更新 agent.md
You don't need to open an editor every time you want to add a new rule. What I do now is ask the agent to update agent.md.
你不需要每次想要添加新规则时就打开编辑器。我现在的做法是让 agent 来更新 agent.md。
*
*
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力