最新资讯
dev.to 我们在 DolphinBench 上测试记忆检索:纯关键词搜索暴露了自身回归 dev.to 别再把 Agent 写成 while 循环,把它建模为状态机 bloomberg.com Rightmove 与 Scout24 借 AI 产品应对低迷房市并上调展望 stackoverflow.blog 欢迎来到芯片时代:半导体制造、AI 数据中心与芯片设计的未来 www.youtube.com AI Agent 总犯同样的错:CentralAgent AI 将记忆重构为经验系统 pub.towardsai.net Netflix 科学家建议重采样 LLM 评判:5 次仅将误差棒缩小 13% pub.towardsai.net Jev 作为 LLM 前的静默层:构建提示感知的模型路由器 reddit.com OpenAI 称 Astra 仅满足 10% 安全任务标准 dev.to FreeToken:在自有 GPU 上运行前沿 MoE 模型及其内存需求 dev.to 当 AI Agent 说“完成”时,究竟完成了什么? dev.to 部署 Underdog Bark 3.0 Pro:逐字转写、自定义后端与分块设置 dev.to 为艰难对话构建 AI:五个 Agent、一条安全正则与一位盲评监督者
多元动态
登录 注册
智能体 dev.to · @Blaise “Mawulolo” Akpalu

如何给 AI 编码 Agent 提供上下文,而不必交出整个代码库

原文 我的收藏

作者指出,给 AI 编码 Agent 更多上下文并不总意味着更好结果:把整个仓库、全部配置、数据库 schema 和 API 端点都交给 Agent,会引入噪声、抬高 token 消耗、暴露与任务无关的实现细节,反而让重点更难识别。作者主张把上下文视为需要刻意设计的东西,而非越多越好。文章为个人实践观点,未提供具体工具链或量化对比。

AI推荐理由 文章讨论给 AI 编码 Agent 提供上下文的方法论:不应把整个代码库塞给 Agent,因为无关文件会引入噪声、增加 token 消耗、暴露无关实现细节,主张把上下文当作需要刻意设计的东西。对使用编码 Agent 的开发者有实用参考价值;但为个人经验分享,摘要未给出具体工具、流程或对比数据。

63

正文

AI coding agents are becoming remarkably good at working inside real codebases. Give an agent enough information and it can understand your components, follow existing patterns, trace data flow, create new features, refactor code, and even reason about architectural decisions. But there is a problem: «More context doesn't always mean better results.» Giving an agent access to your entire codebase may seem like the best way to help it understand your project. In practice, unnecessary files can introduce noise, increase token usage, expose implementation details that aren't relevant to the task, and make it harder to identify what actually matters. I've been thinking about this as I work more with AI coding agents, and I believe the better approach is to treat context as something you intentionally design. The Context Problem When working with an AI coding agent, it's tempting to give it everything: The entire repository Every configuration file All database schemas Every API endpoint Environment variables Authentication logic Old implementations Unrelated features Internal business logic The thinking is simple: «"If the agent knows everything, it can make better decisions."» But software engineers don't usually work this way. When you join an unfamiliar project, you aren't typically handed the entire repository and told: «"Figure it out."» You're given a task, some background, the relevant files, and the conventions you need to understand the problem. AI agents can benefit from the same approach. Start With the Task Before giving an agent access to a collection of files, define what you're actually asking it to do. For example, suppose you want to add a contributor submission feature. The agent probably doesn't need: Your entire admin dashboard Your payment system Your analytics implementation Every unrelated database table Your deployment configuration It may need: Task │ ├── Contributor submission requirements ├── Existing article model ├── Submission API/server action ├── Authentication/session interface ├── Relevant UI components └── Validation conventions Enter fullscreen mode Exit fullscreen mode That's enough to establish a useful working boundary without making the entire repository part of the problem. Separate Interfaces From Implementations One useful way to reduce unnecessary context is to distinguish between: What a system does and How it does it. For example, an agent may need to know that your application has an authentication system: const session = await getSession(); if (!session?.user) { return unauthorized(); } Enter fullscreen mode Exit fullscreen mode It may not need to understand the entire implementation of: Session storage OAuth providers Cookie configuration Database adapters Token handling Internal authentication utilities The interface tells the agent what it needs to use. The implementation explains how everything works underneath. Unless the task requires changing authentication, exposing all of those details may add complexity without improving the solution. Think in Boundaries A useful mental model is to treat your application as a collection of boundaries. ┌─────────────────────┐ │ UI Layer │ └──────────┬──────────┘ │ ┌──────────▼──────────┐ │ Feature Logic │ └──────────┬──────────┘ │ ┌──────────▼──────────┐ │ Data / API Layer │ └──────────┬──────────┘ │ ┌──────────▼──────────┐ │ Infrastructure │ └─────────────────────┘ Enter fullscreen mode Exit fullscreen mode If you're asking an agent to modify a UI component, it may only need the UI layer and the relevant interfaces from the layers below it. If you're changing database behaviour, you'll probably need more of the data layer. The important idea is: «The agent's working boundary can be smaller than the application's boundary.» Runtime Boundaries vs Agent Boundaries Your application may legitimately have access to: Authentication Database Payments Users Analytics Admin Storage Email But an agent working on a profile component might only need: Profile component User type Profile API contract Validation schema Relevant UI conventions The application needs the complete system to operate. The agent only needs enough of the system to perform the task. This distinction becomes increasingly important as agents gain more autonomy. Create Sanitized Blueprints Another approach is to create small architectural blueprints that explain how parts of your application work without exposing every implementation detail. For example: Feature: Contributor Posts UI └── ContributorPostForm Server ├── submitContributorPost() └── validateContributorPost() Data ├── contributor_posts └── users Authentication └── getCurrentUser() Enter fullscreen mode Exit fullscreen mode Flow Contributor ↓ Post Form ↓ Validation ↓ Server Action ↓ Database ↓ Admin Notification Enter fullscreen mode Exit fullscreen mode An agent can understand the architecture from this without reading every file involved in the system. You can then provide the actual implementation files when they're required. There's also a useful side effect: «The blueprint becomes documentation for humans too.» Context Can Become a Security Boundary This isn't only about producing better code. It can also become part of your security model. An AI coding agent that can access everything potentially has access to: Sensitive business logic Internal APIs Customer information Database structures Private documentation Infrastructure configuration Secrets or credentials if your environment is poorly configured Even when secrets are properly protected, unnecessarily exposing internal implementation details increases the amount of information an agent can access and reason about. A more deliberate architecture could look like this: AI Agent │ ▼ ┌─────────────────┐ │ Allowed Context │ └────────┬────────┘ │ ┌─────────▼─────────┐ │ Contracts / APIs │ │ Schemas / Types │ │ Relevant Files │ └─────────┬─────────┘ │ ▼ Application Enter fullscreen mode Exit fullscreen mode This doesn't mean an agent should never access deeper parts of the system. It means access can be intentional and scoped to the work being performed. A Practical Agent Workflow I like thinking about an AI coding task as a gradual process rather than a single prompt. Define task ↓ Identify required files and contracts ↓ Provide relevant context ↓ Ask agent to propose an approach ↓ Review the approach ↓ Expand context if necessary ↓ Implement ↓ Run tests / security checks ↓ Review changes ↓ Merge Enter fullscreen mode Exit fullscreen mode The important part is: «Expand context if necessary.» If the agent discovers that it needs information about another part of the system, provide it. This makes the interaction more deliberate than simply giving the agent unrestricted access from the beginning. This Changes How We Think About AI Coding AI coding isn't only about writing better prompts. It is also about designing better context boundaries. As AI agents become more capable, developers may spend less time manually writing every line of code and more time deciding: What the agent should know Which abstractions should be exposed Which implementation details should remain hidden What constraints the agent should follow How changes should be validated That starts to look less like traditional prompting and more like software architecture. The Bigger Idea I've started thinking about AI coding agents almost like another developer joining a project. You wouldn't give a new developer unrestricted access to every system on their first day. You'd give them: The task The relevant documentation The architectural conventions The interfaces they need The files related to the feature Additional context as their understanding grows AI agents can be approached in a similar way. The goal isn't simply to give an agent less information. It's to give it the right information at the right time. «Good AI-assisted development may depend less on how much context we provide and more on how intentionally we design that context.» A Note on Terminology I'm using "AI coding agent" broadly to describe tools that can inspect a codebase, reason about changes, modify files, and run development tasks with varying degrees of autonomy. The specific capabilities and context mechanisms differ between tools, but the underlying principle applies broadly: «Give the agent the context it needs — not everything you have.» What has your experience been with giving AI coding agents context? Do you give them broad access to your codebase, or do you deliberately scope what they can see?

来源报道 · 1 篇 · 以下链接均前往原始媒体

Blaise “Mawulolo” Akpalu 如何给 AI 编码 Agent 提供上下文,而不必交出整个代码库 09-28 · 原文发布
← 返回精选动态 原文

更多内容