粘贴整个2000行的文件会浪费令牌,而且会降低答案的质量。目的是给AI提供它推理你的问题所需的确切上下文——仅此而已。
粘贴整个2000行的文件会浪费令牌,而且会降低答案的质量。目的是给AI提供它推理你的问题所需的确切上下文——仅此而已。
db is a Postgres pool。"# Bad: paste the whole 800-line user-service.ts
# → AI wades through unrelated code, may anchor on the wrong function
# Good: send only what matters
// file: src/services/user-service.ts
// context: called from POST /users; `db` is a Postgres pool (pg)
type User = { id: number; email: string };
async function createUser(email: string): Promise<User> {
const { rows } = await db.query( // <-- the function I need help with
"INSERT INTO users (email) VALUES ($1) RETURNING id, email",
[email],
);
return rows[0];
}
// Question: how do I handle a duplicate-email conflict here?
第二个版本向AI提供了函数、其返回类型(User)和关于db的一行说明——足以精确回答,不会被无关代码分散注意力。
这不仅仅是成本问题。额外的代码是干扰因素:模型可能会被无关的函数吸引,在文件的其他地方复制bug,或者将注意力分散在数千个无关令牌上。一个聚焦的prompt会产生一个聚焦、准确的答案。
令牌预算是有限的,大型数据转储会降低速度和质量。发送最小相关部分——目标函数、其类型、上下文摘要和文件路径——既能节省令牌,又通过消除干扰来优化答案。策划上下文是与AI有效工作的核心技能:您不是隐藏信息,而是将注意力引导到重要的事情。