MIT Missing Semester 2026,第 7 讲:智能体编码——文字记录
本页标题、字幕、摘要和阅读笔记为已保存的 AI 译文,可能有误。时间戳与来源一致,可展开查看原始字幕,并对照原视频核对。
MIT Missing Semester 演示了智能体编码工具,讲解了 LLM 工具调用循环、上下文与隐私权衡、测试工作流以及常见陷阱。
文字记录来源:创作者上传到 YouTube 的字幕。AI 标点由 Vidleaf 添加;原始措辞和时间戳锚点均保留。原始文字记录见下方。
AI 生成的摘要
本讲延续了“开发环境与工具”一讲中的 AI 驱动开发主题,从自动补全和内联聊天转向编码智能体,并将其描述为一种更复杂、更新颖的形态。编码智能体本质上是一个对话式 AI 模型,可以访问读取和写入文件、执行 shell 命令等工具;演示使用 Claude Code 将一个简单的 Python 脚本 dl.py 改造成使用 argparse 的命令行程序,并通过 mypy 检查类型注解。讲者讨论了安全性和成本,建议对可能改变计算机状态的单个工具调用逐一批准,将所有内容都纳入 Git 跟踪,并指出该演示花费了约 19 美分。智能体大致由两部分组成:底层语言模型,可以将其视为对以提示词字符串为条件的补全字符串概率分布进行建模;以及包裹它的智能体框架,负责处理上下文窗口、轮次标记和调用工具的输出。框架很可能是确定性的,而模型是概率性的;模型并不像人那样智能,有时会输出不太正确的内容,但在实践中这些内容往往运行得还算不错。本讲还涵盖了提示词、通过清除、回退和压缩进行上下文管理、llms.txt 和 agents.md、子智能体,以及一些注意事项:LLM 是概率性的,应审查输出中的正确性和安全漏洞,AI 可能会失控或陷入调试死循环,因此必须加以监督。
根据转录内容生成。请对照原始视频检查关联段落。
带时间戳的章节摘要
使用 Claude Code 进行智能体编程
从 AI 辅助编程到编程智能体
- [0:04] 本讲延续了《开发环境与工具》讲座中的 AI 驱动开发主题,从自动补全和内联聊天转向编程智能体。
- [0:21] 编程智能体被呈现为一种比大约五年前所存在的形态更复杂、更新颖的形态。
- [0:36] 计划是先演示智能体如何工作,然后解释它们在底层是如何运作的,接着介绍基础知识和进阶主题。
演示任务:将 dl.py 转换为命令行程序
- [1:21] 起点是一个简单的 Python 脚本,它从课程网站下载一个页面并从中提取所有链接。
- [1:33] 目标是将这个拼凑而成的脚本变成一个更好的命令行程序,例如加入参数解析,使要获取的页面成为命令行参数。
- [1:48] 可以要求编程智能体端到端地完成整个任务。
工具选择与编程智能体的定义
- [1:54] 演示使用的是 Claude Code,一种特定的编程智能体;其他命令行智能体列在讲座笔记的推荐软件部分。
- [2:08] 编程智能体也内置于 VS Code 或 Cursor 等 IDE 中,提供大致相同的功能。
- [2:32] 编程智能体基本上就是一个对话式 AI 模型,它可以访问帮助其编写代码的工具,例如读写文件和执行 shell 命令。
- [2:50] 有些模型经过专门调优,在编写软件方面特别高效。
自然语言提示词与工具使用
- [3:05] 提示词要求智能体使用 argparse 将 dl.py 变成一个正规的命令行程序,带有正确的类型注解,并用 mypy 进行检查。
- [3:49] 智能体像 ChatGPT 一样产生文本输出,但也能调用工具;在这里它决定先读取 dl.py。
- [4:04] 对于这个简单的任务,它一次性更新了 dl.py,添加了函数以及一个带有命令行参数解析的 main 函数。
- [4:32] 按照指令,它随后通过 shell 调用 mypy,以脚本路径作为参数来检查类型注解。
权限与自主运行
编程智能体:安全性、成本与交互
为安全而批准工具调用
- [5:00] 说话者让编程智能体做大量修改,之后再一次性审查全部改动。
- [5:07] 对于 bash 这类工具,说话者希望检查智能体运行的每一条命令,并确认其安全。
- [5:17] 完全自主的智能体会引发安全担忧,因为它们可能调用不受欢迎的命令,例如删除你在意的文件。
- [5:28] 说话者个人推荐一种模式,要求对可能改变计算机状态的单个工具调用进行批准。
智能体循环与多轮交互
- [5:36] 工具调用获批后,运行框架执行了该工具,将结果返回给模型,模型继续运行脚本并验证其正常工作。
- [8:50] 在单次演示中,一条指令就让智能体调用工具、检查结果、再调用更多工具,并报告完成。
- [9:04] 该智能体还支持多轮交互:它会保留先前对话的上下文,并能对后续消息采取行动,例如移除类型注解。
控制与监控 AI 开销
- [6:14] 开销可以限制,不过具体的配置选项取决于所用工具。
- [6:22] 选择模型是一种控制手段:可能解决更难任务的高容量模型花费更高,而更小、更快的模型更便宜。
- [6:35] 有些服务允许你设置开销上限,例如每月最多 10 美元。
- [6:47] 说话者个人不设开销上限,但会监控用量,例如通过 Claude Code 中的斜杠 cost 命令,演示中的花费约为 19 美分。
- [7:02] 对学生来说,每月 20 美元、包含一定用量的订阅方案可能是不错的起步方式。
成本权衡与本地模型
迭代式编码智能体工作流与防护措施
迭代优化与中断智能体
- [9:27] 你评估结果并告诉智能体以某种方式进行调整,迭代地使用工具。
- [9:37] 如果编码智能体出错、困惑或荒谬地调用工具,你可以中断它,例如在所用工具中按 Control-C,然后给它一条后续消息。
- [9:53] 例如,如果它因为 mypy 安装在异常位置而调用了错误的工具,你可以中断并告诉它 mypy 的位置以及如何调用它。
保护文件免受不必要的更改
- [10:17] 在所使用的特定工具中,显然无法硬编码限制来禁止触碰文件的特定部分。
- [10:27] 一种实用的方法是将所有内容都跟踪在 Git 中,并还原任何你不想要的更改。
- [10:34] 你也可以给出自然语言指令,例如不更改某段代码,智能体通常能很好地遵守这些指令。
与智能体进行测试驱动开发
AI 编码智能体的工作原理:语言模型与智能体框架
解释的范围与目的
- [11:38] 演讲者计划解释底层 AI 模型和智能体框架的工作原理,其抽象程度足以有效使用这些工具,而非重新实现它们。
- [12:18] 目标是传达这些工具擅长什么、它们的局限性,以及为什么某些功能在智能体框架中实现。
语言模型作为条件概率分布
- [12:33] AI 编码智能体大致可分为两个主要部分:底层语言模型和包裹该模型的智能体框架。
- [13:04] 大型语言模型可以看作是对以提示字符串 x 为条件的补全字符串 y 的概率分布进行建模,写作 pi theta of y given x。
- [13:55] 使用这些模型意味着在给定输入的情况下,从该参数化分布中采样输出字符串 y hat,其他一切均构建在这个核心原语之上。
- [14:28] 在实践中,这些模型具有有限的上下文窗口,这意味着提示词和补全字符串的长度都有上限。
通过轮次标记和完整历史实现多轮对话
- [15:19] 对话式聊天的工作原理是使用轮次标记,并将整个对话历史作为输入提供给模型,模型则输出从分布中采样的下一个内容。
- [16:07] 诸如将一替换为二之类的后续问题本身没有意义,需要先前的上下文才能正确回答。
- [16:28] 对话式聊天应用通过将包含特殊轮次标记编码的整个对话作为提示词,再次从语言模型采样来处理这一问题。
在聊天之上添加工具调用
- [17:25] 除了轮次标记之外,智能体框架还可以对 LLM 输出添加特殊解释,因此模型既可以输出助手文本,也可以输出类似“调用工具”的内容。
通过智能体框架训练 LLM 调用工具
工具调用输出格式
智能体框架执行循环
- [18:20] 包裹语言模型的代码,类似于将完整消息历史拼接为后续输入的对话式聊天应用,充当智能体框架,也称为脚手架。
- [18:43] 框架解释 LLM 的输出(y-hat),在机器上执行它,并将结果作为后续输入反馈给语言模型。
将工具输出反馈给模型
智能体框架与 LLM 的交互
智能体框架的核心功能
- [19:39] 智能体框架实现了在循环中调用 LLM 的代码,接收其输出,并在用户机器上分发和执行任何请求的工具调用。
- [19:52] 它将工具产生的文本反馈给语言模型,以便模型能够产生另一个输出。
- [20:06] 大致来说,这个框架接收用户输入,在循环中调用智能体,执行请求的工具,并把结果反馈回去,直到模型输出面向用户的文本输出。
- [20:32] 当模型表示已完成时,控制权交还给用户。
工具实现与编码能力
- [20:32] 工具实现可能包括用于执行脚本的 bash,以及文件读取、写入和编辑工具。
- [20:59] 这些工具共同让底层 AI 模型能够在机器上进行更改,并以某种智能的方式开发代码。
- [21:05] 智能体可以运行测试、查看结果,并在决定后续动作时将其纳入考虑。
语言模型与框架作为主要原语
- [21:22] 核心是一个语言模型,它对字符串或 token 的概率分布进行建模。
- [21:28] 智能体框架在循环中调用 LLM,进行反复采样,分发工具请求,运行它们,并将结果反馈回去以推动进展。
- [21:41] 当用户输入查询时,系统通常会在底层多次从语言模型采样,同时运行一系列工具。
框架与模型之间的互操作性
- [22:00] 智能体框架与底层语言模型之间通常存在常用接口,尽管它们并未完全标准化。
- [22:14] 如今已有相当多的互操作性;例如,像 OpenCode 这样的开源智能体框架可以与不同的底层语言模型配合使用,例如 Anthropic 和 OpenAI 的模型。
确定性框架与概率性模型
Claude Code 中的智能体框架工具调用循环
设置:使用 Opus 4.5 的 Claude Code
- [24:01] 演讲者启动了一个智能体框架,具体是由 Anthropic 开发的 Claude Code,底层运行 Anthropic 的 Opus 4.5 大语言模型。
- [24:15] 初始用户查询作为第一个查询给出,显示在示例的右上角。
读取文件工具调用与上下文增长
- [24:20] 模型输出中间文本,然后以参数 dl.py 调用读取文件工具。
- [24:30] 智能体框架将模型输出解释为工具请求,在文件系统上执行文件读取,并将文件内容作为额外输入反馈给语言模型。
- [24:49] 这会不断向对话历史 X 追加内容,因此在此示例中最后发送给 LLM 的就是该文件的内容。
通过差异和类型检查器写入文件
- [25:14] 经过又一次推理后,模型输出更新并请求文件写入工具,提供一个差异,智能体框架将其应用到文件系统上的文件。
- [25:51] 文件更新操作返回成功值,该值在又一次推理前被传回给模型。
- [25:59] 然后模型调用类型检查器 mypy;框架运行它,输出(成功,未发现问题)被传回给模型进行又一次推理。
每个用户提示的多次推理步骤
编码智能体的隐私与用例
云推理与数据隐私
- [26:52] 能更好地执行复杂任务的更高容量语言模型需要大量硬件进行推理,因此它们大多在云端运行。
- [27:05] 除非您与服务提供商有特殊协议,否则使用这些模型意味着将您的数据发送给他们,他们可能会用这些数据进行训练。
- [27:11] 如果隐私是个问题,请详细查看条款和条件,或找到适合您的设置。
Claude 和智能体框架的运行位置
- [27:30] Claude 不一定同时是 LLM 和智能体框架;有几种不同的设置。
- [27:36] 在这种情况下,智能体框架的一个组件可以被认为与模型一起在云端运行,而模型肯定在云端运行。
- [27:42] 开源智能体框架可以完全在本地运行,但所有 LLM 推理步骤仍会将数据发送到云端。
开放式编码智能体指令
- [27:55] 编码智能体可以通过非常具体的指令实现新功能,或者您可以从开放式开始并在此基础上迭代。
- [28:08] 在课程网站(即 missing semester 网站)的 Git 仓库中,可以告诉 Claude 类似“让它看起来复古一点”这样的要求,这大约需要一两分钟。
用智能体修复代码问题
- [28:22] 编码智能体的另一个绝佳用途是让它们修复你代码中的问题。
用编码智能体借助测试修复缺陷
把编译器、linter 和测试失败交给智能体处理
- [28:27] 如果代码无法编译,或者 linter 或其他错误检查工具报错,你可以让编码智能体来修复该问题。
- [28:42] 对于缺陷,一个很好的第一步是为它编写一个测试,这个测试同时也可作为回归测试,防止将来再次出现损坏。
- [29:01] 一旦有了一个失败的测试,你就可以告诉编码智能体该测试失败了,并让它找出问题并让测试通过。
演示准备:一个手写的失败测试
智能体复现缺陷并阅读代码
- [29:51] 对测试满意后,用户启动编码智能体,告诉它存在一个缺陷,给出复现该缺陷的命令,并让它修复该缺陷。
- [30:21] 智能体首先通过 bash 运行测试来复现缺陷并确认失败,但仅凭输出信息不足以修复它。
- [30:40] 随后智能体使用文件读取和正则搜索工具来阅读并理解代码,演讲者指出这些智能体非常擅长遍历代码库。
速度限制与自主验证
- [31:13] 编码智能体并不是最快的工具,让它们既有效又更快是一个活跃的研究领域;该演示已经运行了大约一分钟。
- [31:35] 模型给出其诊断和修复方案,然后自行请求重新运行测试,用户批准后,测试通过。
- [31:50] 智能体还会运行其余测试,以检查它没有破坏代码库中的其他内容,然后总结它做了什么。
按仓库风格提交更改
根据 git diff 和日志生成 AI 提交信息
检查 diff 和最近的提交风格
编写提交信息
- [32:53] 它会编写一条看起来相当合理的提交信息,例如指出该更改修复了某个特定的 GitHub 问题。
为编码智能体和 Claude Code 编写提示词的使用
提示词和模型对代码质量的影响
- [33:22] 提供给编码智能体的提示词以及底层 LLM 绝对会影响代码质量、冗长度、任务时长以及类似结果。
- [33:33] 目前,为 LLM 编写好的提示词与其说是科学,不如说是艺术,而且底层模型也在不断改进。
- [33:40] 较旧的模型可能仍能完成此类任务,但需要非常详细的说明,而当前的模型只需被告知有一个 bug 需要修复即可。
针对复杂功能的高级提示词模式
- [34:00] 许多关联资源介绍了如何为各类任务有效地向这些工具编写提示词,包括实现复杂功能。
- [34:10] 像让某物看起来有复古感这样的简单请求只需要简短的提示词,而复杂、严肃的功能可能需要长达数段的提示词。
- [34:22] 一种高级模式是像产品设计师或产品经理那样编写一份完整的 PRD,将 markdown 文档交给编码智能体,并要求它阅读该文档、制定实现计划并加以实现。
目录范围与 Git 仓库操作
- [34:58] 粗略来说,在某个目录中启动的编码智能体会看到该目录及其子目录中的所有内容,而某些智能体还可以请求查看其外部的文件。
- [35:12] 这些细节在这个相当新的领域中因众多工具而异,但通常你会希望在 Git 仓库这一层级上操作。
课程网站上的 Claude Code
- [35:43] Claude Code 被用于课程网站的若干更改,部分是为了内部试用该技术,包括在讲座被拆分出来成为独立讲座时进行的重构。
- [36:03] Claude Code 做得不错但并非完美:它没有把所有练习都放到完全正确的位置,有些练习消失了,不得不恢复,而一个关联的 GitHub 拉取请求显示了哪些内容是手动修复的。
- [36:26] Tufte 风格的边注也是用 Claude Code 实现的,据说 Jon 自己一行代码都没写。
创意前端工作的氛围编程
- [36:33] 对于创意工作,给智能体一个高层次的描述、审阅结果并给出调整反馈可能非常有效,现在许多人就是这样进行前端和网页设计的。
编程智能体的使用场景
重构与代码审查
- [37:14] 编程智能体对重构很有用,Claude Code 曾被用来重构讲义;同样的工具也可以重构代码。
- [37:25] 这些智能体非常擅长代码审查:对于 GitHub 拉取请求或个人使用,只需让智能体审查代码就能得到有用的反馈,而更具体的指导会产生更贴合需求的回应。
- [37:43] 演讲者曾使用这些工具在进行风格调整或性能优化(旨在保持代码语义等价)时捕捉细微的缺陷,因为智能体能够很好地推理代码和差异,并能指出行为实际发生变化的地方。
代码库理解
编程智能体作为 shell、学校工具和氛围编程
将编程智能体用作自然语言 shell
- [38:59] 由于编程智能体可以调用工具,它们可以在某种程度上像 shell 一样使用,从而避免记忆 shell 命令及其标志。
- [39:14] 无需回忆语法,你可以用自然语言描述任务,例如让智能体使用 ag 查找所有带有重命名导入的 Python 文件。
- [39:54] 你可以给出后续指令,例如忽略 lib 目录,智能体将生成并运行正确的 shell 命令,甚至总结结果。
学校政策与学术诚信问题
- [40:17] 在学业中使用这些工具是否会给你带来麻烦因课程而异,因此学生应询问导师是否可以。
- [40:25] 有些课程禁止任何 AI 编程工具,包括 AI 自动补全、内联聊天和编程智能体,而另一些课程则允许使用任何工具。
- [40:41] 演讲者建议遵守规则,不要在作业中作弊。
AI 工具作为类似助教的帮手
- [40:50] Anthropic、OpenAI 等公司已推出针对学业任务的特殊功能或模式,它们不会直接给出答案,而是更像助教一样行事。
- [41:04] 为了优先培养你自己的理解,你可以向工具请求提示,例如让它在不透露答案的情况下为你指明某个 bug 的方向。
- [41:12] 演讲者表示,这些工具在这方面表现尚可,可以作为助教的替代品使用。
氛围编程整个项目
高级智能体功能:可复用提示词
保存并复用提示词
并行智能体与 git 工作树
为何要并行运行智能体
- [43:40] 即便是相当简单的任务,模型也可能需要几分钟来思考和推进。
- [43:47] 借助更高级的功能,模型已被观察到能在代码库上工作 20 或 30 分钟,因此你不会想只是干坐着盯着看。
- [43:55] 你可以启动多个智能体副本,并行处理一个 bug 修复和一个或多个功能。
- [44:04] 在本讲中,不同的智能体运行在不同的代码仓库上:一个 Claude 将课程网站改成复古风格,另一个则修复了代码库中的一个 bug。
使用 git 工作树隔离智能体
用于智能体与工具连接的模型上下文协议
MCP 作为连接执行框架与工具的标准
- [45:35] 存在一个名为模型上下文协议(Model Context Protocol)的标准化协议,用于将智能体执行框架与工具连接起来。
- [45:43] 这些连接有多种开源选项以及一些托管选项可供选择。
示例:将编码智能体连接到 Notion
编码智能体中的上下文管理功能
有限的上下文窗口与被污染的上下文
- [46:29] 底层 AI 模型的上下文窗口是有限的,通常有一个固定的上限,例如 100,000 个输入 token。
- [46:43] 即使输入在技术上处于上下文窗口之内,模型处理起来也更困难,从而产生被污染的上下文。
- [46:57] 有效管理提供给底层 AI 模型的输入,通常能提升其性能。
清空上下文窗口
- [47:04] 智能体执行框架支持清空上下文窗口等基础功能,因为聊天通常会不断将消息、工具调用和工具输出追加到消息历史中。
- [47:25] 当切换到不相关的任务时,你可以终止并重启智能体,或使用命令清空上下文窗口并重新开始。
回退对话
- [47:42] 智能体执行框架可以通过从消息历史末尾弹出消息并将其从上下文中删除来回退对话。
- [47:50] 用后续指令打断智能体会把自最后一条消息以来所做的所有事情都留在上下文中,这令人困惑且不太有用。
- [48:15] 回退有助于保持较小的上下文窗口,并让 LLM 更专注。
长历史的压缩
用于模型知识截止日期的 LLMs.txt
知识截止日期与获取库文档
- [50:03] 编码智能体中使用的 AI 模型在时间上是冻结的,训练数据仅包含训练时已存在的数据,因此它们存在知识截止日期。
- [50:27] 如果要求使用在截止日期之后发布的库来实现某个功能,模型对该库一无所知,因此必须在推理时将库的相关信息提供到模型上下文中。
- [50:48] 语言模型可以从互联网获取代码;带有网页获取工具的编码智能体可以检索诸如 missing.csail.mit.edu 这样的 URL,将内容带入其上下文窗口,并据此采取行动。
LLMs.txt 作为纯文本标准
- [51:27] llms.txt 是一项为 LLM 格式化信息的提议标准。
- [51:34] 获取原始 HTML 会给模型提供冗长的内容,包括与任务无关的样式和分析信息。
- [51:59] LLMs.txt 是一种纯文本文件标准,旨在供 LLM 在推理时使用,许多库(尤其是与 AI 相关或具有前瞻性的库)都会发布一个。
Pydantic AI 示例与上下文节省
使用 agents.md 为编码智能体提供仓库上下文
agents.md 的用途与设置
- [52:47] 可以将一个名为 agents.md 的准标准文件放置在 Git 仓库或项目中,以便编码智能体在每次启动时读取其全部内容。
- [53:02] 对于 Claude,该文件名为 claude.md,讲义或课程仓库中提供了一个示例。
避免在空上下文中重复指令
- [53:18] 当编码智能体以完全空的上下文启动、对仓库一无所知时,该文件非常有用。
- [53:31] 它可以记录诸如运行类型检查器的命令或运行单元测试的命令等实践,这样就不必在智能体每次启动时都提供这些信息。
可复用提示词与长期仓库
技能作为按需上下文管理
大型 agent.md 文件的局限
- [54:09] 技能是作为 agent.md 文件的后续发展而出现的,后者让编码智能体在启动时了解项目的一切。
- [54:24] 一个非常大的 Agents.md 会在任何对话开始之前就消耗掉底层大语言模型上下文窗口的很大一部分。
- [54:36] Agent.md 文件涵盖了许多主题,但编码智能体在任何给定时刻可能并不需要所有这些信息都处于上下文中。
通过按需技能文件实现间接寻址
编码智能体中用于上下文管理的子智能体
子智能体作为上下文管理机制
- [55:42] 子智能体在一定程度上与上下文管理有关:编码智能体可以为一个已识别的子任务派生一个独立的智能体,只针对该子任务给它一个提示词,并在它完成后重新获得控制权。
- [56:02] 由于子智能体会将控制权交还给父智能体,父智能体的上下文不会被该子智能体所做的一切细节所污染。
- [56:12] 你可以为这些编码智能体编写自己的子智能体,它们有时也被用来实现智能体自身的组件。
WebFetch 作为子智能体实现
- [56:21] 一些编码智能体将 WebFetch 工具实现为子智能体,因为将网页的整个 HTML 加载到上下文窗口中可能并不是最好的主意。
- [56:38] 一个独立的智能体可以抓取网站并总结相关方面,这样只有经过筛选的相关信息进入父智能体的上下文,上下文窗口也能保持更小。
并行子智能体以提升速度
- [56:54] 这些智能体通常支持并行工具调用和并行子智能体,使多个任务能够同时运行。
- [57:02] 例如,可以针对某个功能并行派生一个带有强代码审查提示词的子智能体,以及另一个带有强类型检查问题修复提示词的子智能体。
- [57:15] Claude Code 可以做到这一点:两个子智能体同时工作,父智能体在两者都完成后继续,这也能加快你的速度。
通过笔记和练习学习这些概念
在压缩上下文与开启新会话之间做决定
压缩上下文作为延续同一线程的艺术
为无关工作清空上下文
- [58:29] 如果接下来的工作无关,就清空上下文,而不是保留一堆无用的上下文,即使它是一份摘要。
AI 编程工具的注意事项与局限
LLM 是概率模型,而非魔法
- [58:47] AI 工具很强大,但 AI 并非魔法,也会犯错。
- [58:55] LLM 是概率模型,给定输入 token 后,会返回下一个内容的条件概率,不过训练出一个好模型还需要巧妙的技术。
- [59:07] LLM 并不像人类那样具有智能。
审查 AI 输出的正确性与安全性
- [59:11] 用户应审查 AI 输出的正确性和安全漏洞。
- [59:17] 演讲者经常看到 AI 生成的代码乍看合理,实则微妙地出错,而找出这个 bug 要花大量时间。
- [59:25] 验证代码有时比你自己直接写出正确的代码更难,因此对于真正关键的事情,演讲者常常亲自完成任务,而不是调用 AI 工具。
煤气灯效应和调试螺旋等失败模式
- [59:41] AI 可能会失控、钻牛角尖,并在被告知出错时坚称自己正确,试图对你进行煤气灯操纵。
- [59:52] AI 可能陷入调试螺旋,反复修复代码并运行每次都会失败的测试,使代码越来越糟,因此必须加以监督。
计算机科学与编程技能的持续价值
前 3 段双语对照预览 · 免消耗额度
以下展示前 3 段已保存中文译文与英文原版字幕的逐段对照(零额度消耗):
好的,欢迎来到 Missing Semester 的第七天。今天我们要讲智能体编码,延续上周在开发环境与工具讲座中涉及的一些 AI 驱动开发的话题。上次那节课我们讲了一些 AI 辅助编码,比如自动补全和行内聊天。今天我们要讲的是我认为更复杂、更新颖的东西,一种与五年前存在的形式不同的形态,也就是编码智能体。
All right, welcome to day seven of Missing Semester. Today we're going to talk about agentic coding, so continuing some of the topics on AI-powered development that we were covering in the Development Environment and Tools lecture last week. So last time in that lecture we talked about some AI-assisted coding like autocomplete and inline chat. Today we're going to be talking about something that I think is much more sophisticated and new, a different form factor than what existed, say, five years ago. which is coding agents.
我想在这节课一开始先给你们演示一下这些东西是怎么工作的,然后我们再讲它们在底层是如何运作的,接着讲基础知识,最后讲更高级的话题。这就是今天的安排。那么对于最初的演示,我实际上会继续用我们在开发环境与工具讲座中做的那个例子。回想一下,我现在把代码调出来放在屏幕上。回想一下,我们当时在写这个小脚本,用来……
And I think right at the start of this lecture, I'm going to show you a demo of how these things work, and then we'll talk about how they work under the hood, and then talk about basics, and then more advanced topics. So that's the plan for today. So for the initial demo, I'm going to actually continue with the example we were doing during the development environment and tools lecture. So recall, I'll actually bring up the code here on the screen. So recall that we were working on this little script to...
哦,这不是我想要的。稍等一下,我需要做一点设置。好,回想一下,我们当时在写这个脚本,用来从课程网站下载一个页面并提取其中的所有链接。这就是我们目前的进度。一个非常简单的小 Python 脚本。假设我们想把这个随手拼凑的脚本变成一个稍微好一点的命令行程序。
Oh, it's not what I wanted. One second, I need to do a little bit of setup. Okay, so recall that we were working on this script to download a page from the course website and extract all the links from it. And this is where we're at. So pretty simple little Python script. And suppose we wanted to take this script we hacked together and turn it into a slightly nicer command line program.
[0:01] 好的,欢迎来到 Missing Semester 的第七天。今天我们要讲智能体编码,延续上周在开发环境与工具讲座中涉及的一些 AI 驱动开发的话题。上次那节课我们讲了一些 AI 辅助编码,比如自动补全和行内聊天。今天我们要讲的是我认为更复杂、更新颖的东西,一种与五年前存在的形式不同的形态,也就是编码智能体。
[0:32] 我想在这节课一开始先给你们演示一下这些东西是怎么工作的,然后我们再讲它们在底层是如何运作的,接着讲基础知识,最后讲更高级的话题。这就是今天的安排。那么对于最初的演示,我实际上会继续用我们在开发环境与工具讲座中做的那个例子。回想一下,我现在把代码调出来放在屏幕上。回想一下,我们当时在写这个小脚本,用来……
[1:05] 哦,这不是我想要的。稍等一下,我需要做一点设置。好,回想一下,我们当时在写这个脚本,用来从课程网站下载一个页面并提取其中的所有链接。这就是我们目前的进度。一个非常简单的小 Python 脚本。假设我们想把这个随手拼凑的脚本变成一个稍微好一点的命令行程序。
[1:40] 也许我们想做一些事情,比如添加参数解析,把要获取内容的页面变成一个命令行参数,诸如此类。这样我们就可以让编码智能体端到端地为我们完成整个任务。所以在今天的课上,我会使用 Claude Code,这是一个特定的编码智能体。外面还有很多其他工具。今天的讲义底部有一个推荐软件部分。它们都是命令行编码智能体,至少我个人目前更喜欢使用这种。也有内置于 IDE 中的编码智能体。
[2:11] 所以,如果你使用的是 VS Code 或 Cursor 之类的工具,你可以在 IDE 内获得大致相同的功能。这里我已经启动了 Claude Code。我刚刚在存放代码的目录中打开了一个终端,然后输入 Claude,这就调出了 Claude Code 的命令行界面。而编码智能体基本上就是一个对话式 AI 模型,它能访问一些帮助它编写代码的工具。所以,如果你以前用过 ChatGPT 之类的东西,这就大致相当于 ChatGPT,只不过这个 AI 模型可以执行读取和写入文件、在你的计算机上执行 shell 命令等操作。
[2:50] 而且有时候这些模型会经过专门调优,使其在编写软件方面特别高效。所以我在这个目录里有一个 dl.py 脚本,我可以直接给编码智能体输入自然语言,比如说,把我写的脚本……这里,我是故意打错字的,就让它保持这样。把我写的脚本 dl.py 改造成一个正式的命令行程序,使用 argparse 进行参数解析,也许我还想在其他方面让它更好一些,比如这里我漏掉的一些东西。哦,我想我这里已经有类型注解了,但我可以说,确保它有正确的类型注解,用 mypy 检查它能正常工作。
[3:36] 所以我想检查类型注解,确保它通过类型检查。于是我可以给它这样一条命令,然后它就开始思考了。接下来让我解释一下它的输出。它会给出一些文本输出,就像 ChatGPT 可以给你回文字一样。但它也可以调用工具。所以这个模型决定读取 dl.py 文件。这是开始这项任务的一个合理方式。而且这是一个相当简单的任务。所以它只是更新了这个 dl.py,一次性写好了所有代码。
[4:09] 所以我可以进去看看它做了哪些更改。如我们所见,它写了更多函数。现在它有了这个 main 函数,其中包含命令行参数解析等内容。所以它已经完成了我要求它做的整个任务。而且它还在继续遵循我的指令。回想一下,我告诉过它要确保有类型注解,并且让类型检查器确认一切正常。所以这里它在尝试调用下一条命令。它正在使用 shell 调用 mypy,并把脚本的路径作为参数传给它。
[4:41] 我们在这里注意到一件事:当它编辑文件时,它没有向我请求权限。它直接就去编辑了。这是因为我就是这样配置我的编程智能体的。因为我发现,如果它每对文件做一点小改动都要请求权限,那会很烦人。我希望它能更自主地运行。而且一般来说,我会用 Git 跟踪所有内容。所以我可以让编程智能体做一大堆修改,然后再一次性审查。但对于编程智能体想要调用的其他工具,比如 bash,我希望确保我确实检查了它运行的每一条命令,并确认它是安全的。
[5:14] 我们今天不会深入讨论智能体安全的细节,但如果你让这些东西完全自主运行,就会有一些安全隐患。它们可能会做一些事情,比如调用你不想让它调用的命令。它可能会说删除一个你在意的文件,或者做比这危险得多的事情。至少我个人建议你以这种模式运行它们:你必须批准那些可能改变你计算机状态的单个工具调用。所以我已经批准了那个工具调用。智能体框架运行了该工具。它把结果返回给 AI 模型。模型说一切看起来都不错。然后它决定继续,并想要运行脚本,确保它似乎能正常工作。
[5:50] 所以它在这里又做了一些事情。然后,好了。它完成了我要求它完成的任务。这就是 AI……对,有问题吗?对,所以问题是,这背后在使用某个托管 AI 服务。你能限制自己花多少钱吗?答案是,是的,可以。具体取决于工具,配置项会有所不同。
[6:22] 一般来说,你可以选择要使用哪个 AI 模型,然后更高容量的模型可能更擅长解决更难的任务,但也会更贵。而更小、更快的模型会更便宜。所以这是你可以控制的一个方面。某些服务实际上会稍微限制支出。比如你可以说,我这个月最多想花 10 美元。对。所以问题是,我会限制支出吗?至少我个人不会。我会留意使用情况。比如在 Claude Code 中,有一个斜杠 cost 命令。
[6:53] 到目前为止,我们做的这件事大约花了 19 美分。所以我会持续关注这一点,确保费用不会变得太离谱。我认为如果你是学生,一个不错的起步方式可能是选择每月 20 美元的订阅套餐,然后套餐里包含一定量的使用额度。如果你超出了那个使用量,就要考虑你如何使用 AI。我知道这一点。在很多公司里,人们发现这些工具深受工程师喜爱,并且能大幅提高生产力,以至于相比工程师的时间成本,工具成本相对较低。
[7:26] 所以我知道很多人每月在 AI 使用上花费很多,比如几百美元,但对他们的用途来说这没问题。对,有问题。是的。编码智能体要花钱,而且粗略来说也需要联网,是的。你也可以使用在本地运行的编码智能体,并使用在你自己的笔记本电脑上运行的模型。这其中有取舍。
[7:57] 你必须使用能力低得多的模型,除非你……基本上,你必须使用比我在这个演示中使用的模型能力低得多的模型。然后它们对于简单任务可能还行,但对于更难的事情,它们就不太管用了。而且,我认为就当下而言,使用某个托管模型并为其付费是合理的。是的,问题是关于订阅还是一次性付费。我认为它们都是订阅制,因为推理成本只取决于使用量。然后定价模式各不相同,你可以用谷歌搜一下就能找到。
[8:30] 我认为我们不想在这节课上花太多时间讨论定价。我们要讲的是编码智能体在概念上是什么、如何有效地使用它们等等。好。所以在这个例子中,我向你们展示了一个非常高层面的概览。演示了如何使用编码智能体,展示了我们如何与工具对话。这里它把所有事情都做对了。所以我只是做了这个一次性的事情。我给了它一条指令。它做了一堆事情,包括调用工具、查看结果、根据发生的情况调用更多工具。最终它回来对我说,好的,我做了你要求的事情,结束。
[9:01] 你还可以与这些工具进行多轮交互。我实际上可以移除类型注解,它仍会继续运行。它拥有之前对话其余部分的上下文,我可以像这样发送后续消息。现在这有点傻,它反而让我们的程序变得更糟了。但它确实完成了任务。更实际的做法是,你会对你正在处理的东西进行迭代。如果你正在开发一个功能,而你不完全确定该功能的规格应该是什么,你可能会给它一个粗略的描述。它做了一件事。然后你评判结果。就像,哦,实际上,我想让你这样或那样调整一下。你可以以这种迭代方式使用这些工具。
[9:37] 你还可以做的另一件事是,如果编码智能体出错了,要么是它做错了事并感到困惑,要么是你看到它偏离正轨、思考了很多或者以毫无意义的方式调用工具,你甚至可以打断它,比如在我使用的这个工具中按 Control-C,然后给它发送一条后续消息。所以,如果它调用了错误的工具,比如也许我把 mypy 安装在了某个奇怪的位置,它找不到,我可以直接打断它并说,实际上,mypy 在这里。这样调用它。例如。那么关于这一点有什么问题吗?
[10:10] 是的。所以问题是,你能硬编码诸如不要碰这个文件或不要碰文件的这一部分之类的事情吗?在我使用的这个特定工具中,我认为你无法硬编码不能碰文件的某个特定部分。实际上,我会这样处理这类事情,我认为就是像在 Git 中跟踪所有内容,看看它改了什么。比如,如果它改了你不想改的东西,就还原它。你也可以给它自然语言指令。比如你可以说,移除所有类型注解,除了这个函数中的,或者像不要改动这块代码。
[10:42] 而且它通常能很好地遵守这些指令。是的。所以问题是,我是否有使用智能体进行测试驱动开发的经验?答案是有的。我想我们会在讲座稍后一点讲到这个。简而言之,这是使用这些工具的一个相当不错的方式。要么你自己写测试,然后告诉智能体去做那件事,比如实现该功能使其通过测试,要么你甚至可以使用编码智能体来帮你先写测试,然后你可以要求它实现通过测试的功能。
[11:15] 这些东西与反馈循环配合得非常好。所以如果你能设置好它们,比如,也许你有一些无法编译的代码,或者一些无法通过类型检查的代码,或者类似的情况,你可以说,这是错误,这是你可以运行来复现错误的命令,然后不断尝试直到修复它。这与测试驱动开发也配合得很好。好的,所以我们会更多地讨论如何在不同的用例中实际使用这些工具等等。但我今天想涵盖的一个主题是稍微谈一下底层 AI 模型以及这些智能体框架是如何工作的。
[11:51] 现在,这是一个非常深入的研究领域。我显然不会在我计划花在这上面的 10 分钟内完整地涵盖这个主题。我可以花一整个学期来深入探讨这个话题,但我想我会尝试在一个恰到好处的抽象层次上来讲,让你了解这些工具中你需要掌握的方面,以便能够有效地使用它们。所以我不会教你如何自己重新实现这些东西,但我会尝试分享一些关于它们如何工作的内容,让你理解它们擅长什么、有什么局限性,以及为什么人们在智能体框架中实现了某些功能等等。
[12:31] 所以 AI 编程智能体大致可以分为两个主要部分。有一个底层的语言模型,然后在语言模型之上包裹着一个智能体框架。我不知道大家对语言模型有多少背景知识。也许如果你以前用过 ChatGPT 或任何类型的 AI 语言模型,可以举手。好的,很好。好的,所以大多数人都举手了。语言模型,有时也称为大语言模型,可以看作是对补全字符串的概率分布进行建模。
[13:08] 我们把这些称为 y。将补全字符串建模为提示字符串的函数,我们称之为 x。所以补全,补全 y,提示 x。所以它就是一个条件生成模型,给定特定的输入,即一个字符串序列,告诉我们对应于该输入序列的输出字符串上的概率分布是什么。
[13:40] 然后当我们使用这些模型时,我们可以把它建模为一个分布,我写成 pi theta,这个参数化的分布表示给定提示 x 时补全 y 的分布。然后当我们使用这些东西时,我们从这个分布中采样,你可以从给定输入的 pi theta 中采样字符串 y hat。就是这样。这就是 AI 语言模型的全部。
[14:11] 而要找到一个能发挥有用作用的好概率分布,需要投入大量工作。但在实践中,使用它们的方式是给它们输入一堆文本、字符串或词元。然后它们会返回一堆文本作为输出。之后的一切都建立在这个核心原语之上。这里还要注意一个细节:在实践中,这些模型有有限的上下文窗口。基本上,这意味着这个字符串的长度和那个字符串的长度都是有界的。
[14:51] 好,那么在此基础上构建……像 ChatGPT 这样的东西是如何工作的?对话式聊天是如何工作的?我想你大概能想象如何用它进行单轮聊天。比如你在这里给它一个问题,然后在这里得到一个答案。那你会怎么做追问?有什么想法吗?如果你想进行多轮聊天。这些东西的工作方式是使用轮次标记,你把整个对话历史作为输入给模型,它给你下一个东西,也就是模型的输出。
[15:33] 作为概率分布的输出。所以你可能给它输入类似用户说的话,我不知道,比如在黑板上写不下长问题,但比如一加一等于几?然后助手可能会说,所以在第一个查询中,当你从概率分布中采样时,这是你的第一个 X,然后这个东西是你的第一个 Y hat。然后如果你想问一个追问。所以我在这里故意写了一个依赖查询。
[16:10] 比如如果你把那些一换成二会怎样?所以这个查询本身并不是很有意义。你需要上下文才能正确回答这个东西。所以对话式聊天应用处理这种情况的方式是,它会再次从大语言模型采样,哦。把整个这些东西作为提示词,并用某种特殊编码把这些东西作为轮次标记。
[16:43] 所以用户说了这个,语言模型输出是这个,现在用户说了这个,然后那会生成下一个东西。所以这将是你的下一个输出。好,所以我们从大语言模型这个原语——你可以把它看作一个概率分布——走到了如何将它们用于多轮聊天。接下来我们将讨论如何在此基础上构建类似智能体工作台的东西。
[17:18] 那么到目前为止对此有什么问题吗?好。所以你可以再加一个想法,就像这里我们加了轮次标记。现在我们可以对 LLM 的输出添加一些特殊解释。我们可以说模型可以输出类似 assistant 然后一些文本。但我们也可以有类似调用工具的输出。
[17:52] 所以我们将训练一个语言模型,使其能够输出在你的机器上执行操作的请求。因此它可能是……一个例子就是我们在这里给出的那些东西。工具调用的一个例子可能是请求运行,我不知道,比如 mypy、dl.py。然后我们会有一段代码来包装这个语言模型,就像在我们的对话式聊天应用中,我们有一个东西会通过拼接整个消息历史来构建后续输入,我们会有一个智能体框架,有时也称为脚手架,它会解释这些输出。
[18:43] 所以这是来自大语言模型的 y-hat。它会解释这些输出。它实际上会在机器上执行它们,然后将它们作为后续输入反馈给语言模型。所以它会执行这个操作。然后也许还有来自 mypy 工具的所有这些文本输出。比如它说正在检查 dl.py,等等。也许它说第一行错误,期望 int,等等。然后你会把所有这些输出拿来,直接作为另一个输入塞给大语言模型。
[19:16] 所以这构成了你将输入给大语言模型的 X 的后续组成部分。是的,有问题吗?所以问题是,智能体框架到底是什么?它做很多事情。它会实现代码中在循环中运行对大语言模型的调用、接收输出的那部分。
[19:48] 如果有工具调用,它实际上会分派该工具调用。它实际上会在你的机器上执行这个东西。它会获取这个东西产生的文本,并将其反馈给语言模型,让它产生另一个输出。所以……是的,这就是它做的主要两件事。大致来说,智能体框架会接收你的用户输入,然后在一个 for 循环中调用智能体。如果它请求工具调用,它会执行该工具,将输入反馈给语言模型,让它生成另一个补全。
[20:19] 如果又是另一个工具调用,它会执行该工具,将结果反馈给语言模型并产生另一个补全,如此循环,直到语言模型输出面向用户的文本输出,此时……它会说,好了我完成了,控制权回到用户手中,然后你可以继续,所以你会拥有工具实现,就像你在编码智能体工具中可能有的那样,比如 bash,这样你就可以让你的智能体执行 bash 脚本,你可能会有文件编辑、文件读写之类的工具,等等,这些工具的组合让底层的 AI 模型能够真正对你的机器上的东西做出更改,并以某种智能的方式开发代码。
[21:05] 能够运行测试、查看运行测试时会发生什么,并在决定未来采取哪些行动时将这些因素考虑在内。哦,后面有人提问吗?好的。所以这些就是主要的原语。比如核心是一个对字符串或 token 的概率分布进行建模的语言模型。然后还有这个智能体框架,它会在循环中调用 LLM,进行反复采样,还会分发请求去执行工具、真正运行它们,并把结果反馈给语言模型以推动流程向前。
[21:41] 通常当你输入一个查询时,它会运行一堆工具,而在底层,它其实是在对这个语言模型进行很多很多次采样。对,有问题吗?好,所以问题是,智能体框架和底层语言模型之间是否存在一个被充分理解的接口,以便它们能够互操作?是的,有一些常用的接口。它并没有完全标准化、让所有人都用同一个东西,但如今已经有相当程度的互操作性了。
[22:17] 例如,有像 OpenCode 这样的开源智能体框架,你可以让它们与不同的底层语言模型配合工作。它们可以配合 Anthropic 的模型,也可以配合 OpenAI 的模型等等。对,有问题。对,所以你指出的是,语言模型是这种概率性的东西,而智能体框架——大概就像你在框架本身里写的代码——是确定性的,那这些东西之间是什么关系?
[22:57] 它们通过采样产生关联。当智能体框架调用 LLM 时,它是在采样一个输出字符串,然后基于这个字符串去做事情。所以你会调用语言模型,它会输出类似“运行这个命令”的内容,然后你会运行该命令并继续。与这种非确定性相关的是,这个语言模型是以一种聪明的方式训练的,但它并不像人那样智能。它有时会输出不太正确的东西。所以它会调用你计算机上实际并不存在的命令,或者使用这些文件读写工具去写出错误的代码之类的事情。
[23:30] 但在实践中,这些东西往往运行得相当不错。而且随着你使用它们,你会逐渐建立起直觉,知道它们擅长什么、以及它们目前还不足以做什么。对。所以我们已经做过了。对。那么现在我们已经讨论了这些东西如何工作,我们可以回过头再看这个例子,并从底层工作原理的视角来理解它。
[24:01] 那么这里我已经启动了我的智能体框架。在这个特定情况下,我使用的是 Claude Code 智能体框架,它由 Anthropic 开发。而在底层,它使用的是 Anthropic 的 Opus 4.5 大语言模型。我在这里给了它第一个查询。所以这就像右上角的初始用户查询。这里的模型输出了一些中间文本,然后决定调用一个工具。所以这个 LLM 调用了读取文件工具,参数是 dl.py。这个请求传到了智能体框架。
[24:32] 它把那个语言模型的输出解释为,哦,语言模型在请求调用一个工具。它实际上在我的文件系统上执行了文件读取操作,产生了对应文件内容的数据,然后把这些数据作为额外输入反馈给语言模型。所以再想想右上角的例子。这就是往那个 X 里不断添加越来越多的东西。就像我们有一个初始的 X,然后我们不断往这个对话历史里追加内容,直到我们传给 LM 的最新消息或数据。
[25:06] 在这个例子里。所以这里最后发给 LLM 的东西就是那个文件的内容。然后模型,我们运行推理,从模型中采样,现在模型决定输出更多文本。它在给我更新当前进展。同时它还输出了一个调用另一个工具的请求。这次是文件写入工具。在这个特定的智能体框架里,而且我觉得这种做法相当常见。文件写入工具有几种不同的接口,模型实际上可以提供一份 diff,然后应用到文件上。
[25:38] 所以模型再次输出一份 diff,它只是一段文本,然后智能体框架,在这个例子中是 Claude Code,会解释这份 diff 并将其应用到我的文件系统上的文件中。然后它完成了这个操作,又输出了一些中间文本。是的,这个文件更新操作有一个返回值,就像是,哦,操作成功了,所以这个结果被返回给模型,这里我们运行另一轮推理,模型决定调用另一个工具,这次它调用的是 mypy,也就是类型检查器。然后智能体框架解释这个请求,运行类型检查器,产生工具的输出,这次是成功,没有发现问题,这个结果被传回给模型,我们运行另一轮 LLM 推理,以此类推,在它最终输出“我这轮完成了,这是最终文本输出”之前,这里还有一次工具调用。
[26:27],然后控制权交还给用户。所以对于单个用户提示词,这里会发生大量 LLM 推理步骤,这里也会发生大量工具调用,对吧?很好,既然我们在讨论这些底层工作原理,那就简单提一下隐私问题。正如我们之前稍微谈到的,这些能力更强的语言模型往往更擅长执行这些复杂任务,但它们需要相当强大的硬件来运行推理,比如从那个概率分布中采样。
[27:02] 因此这些东西大部分都在云端运行。当你使用它们时,除非你与服务提供商有某种特殊协议,否则你就是在把数据发送给他们。他们可能会用这些数据训练模型等等。所以如果隐私是个顾虑,就去仔细查看条款和条件的细节,或者找到适合你的方案。好,那我们把这节课剩下的时间用来聊聊用例。我想我会试着花……对,有问题。好,问题是,Claude 既是 LLM 又是智能体框架吗?答案是,有几种不同的配置。
[27:36] 在这种情况下,智能体框架的一个组件可以认为与模型一起在云端运行,而模型在这里肯定是在云端运行的。你也可以找到开源的智能体框架,其中框架完全在本地运行,但所有 LLM 推理步骤最终都会把数据发送到云端进行推理。对,很好。好,我们已经给出了一个如何使用编码智能体实现新功能的例子,你可以非常具体地给出指令。你也可以开放式地开始,然后逐步迭代。
[28:07] 例如,我可以进入课程网站(missing semester 网站)的 Git 仓库,启动 Claude,然后说类似“让它看起来复古一点”的话。我们可以看看会发生什么。这需要一两分钟。所以我们稍后再回来看这个。编码智能体的另一个绝佳用例是用它们来修复代码中的问题。所以如果你有编译器问题,比如代码无法编译,或者有 linter 问题——我们周五会更详细地讲 linter——但如果你有一个检查代码中各种错误的工具并且它在报错,你可以让编码智能体帮你修复这个问题。
[28:42] 如果你有单元测试,或者你的代码里有个 bug,至少初步尝试解决这个 bug 的一个好方法就是为这个 bug 构造一个测试。这总体上是一个很好的软件工程实践。如果你的代码出了问题,就写一个测试。这样你之后就可以把它用作回归测试,确保将来不会再出问题。然后一旦你有了测试,你就可以告诉编码智能体,嘿,这个测试失败了,找出问题所在并让测试通过。作为这方面的一个快速演示,演示起来有点难,因为我想在真实的代码库中工作,而你对这个代码库没有上下文。
[29:15] 但我会的,所以不用担心这里的所有细节,但我们会尝试从高层次向你展示事情是如何运作的。好的。所以我这里有一个代码库,不用担心细节,我注意到了代码中的一个问题。在这个特定的情况下,我为那个问题手写了一个单元测试。所以如果我运行这个测试,我会看到测试失败。你也可以使用编码智能体来尝试初步编写测试。
[29:46] 比如你可以启动智能体并说,嘿,当我这样使用代码时,事情就出错了,你能为它写一个测试吗?但一旦你有了一个你满意的测试,你就可以启动你的编码智能体。我要复制粘贴这个命令。你可以启动你的编码智能体并说类似这样的话:有一个 bug。我为它写了一个测试。你可以用这个命令运行它来复现问题,修复这个 bug。
[30:17] 然后就像人类开发者在那种情况下会做的那样,你会先运行测试来复现这个 bug。这就是智能体正在做的。所以它在调用 bash,用我给它的东西来运行我刚写的那个特定测试。然后它查看……输出,看到,是的,这确实失败了。那个输出本身并不是特别有信息量,或者说单靠它本身,不足以修复这个 bug。你需要实际阅读并理解代码。所以这就是智能体接下来要做的。这些东西实际上非常擅长遍历代码库并理解代码,无论理解对 AI 模型来说意味着什么。
[30:50] 所以我们可以看到,好的,它正在读取文件。这是它拥有的另一个工具。它有一个搜索工具,可以用正则表达式模式搜索匹配该模式的不同内容。它在读取更多文件,搜索、读取、搜索、读取、思考。我认为在这个特定情况下,这个 bug 足够简单,它应该能一次性修复这个 bug。让这些编程智能体有点难以演示的另一个原因是,它们并不是最快的东西。这是很多人正在努力解决的问题。比如,你如何让智能体在所做的工作上真正出色且高效,同时运行得更快?
[31:25] 就像我们在这里看到的,哦,这东西已经运行了整整一分钟。但没错,这让演示变得有点棘手。我不能快进这个行为,因为我想做实时演示。但好了,现在模型在说,哦,这是我认为的 bug。在这里,我要用这种方式修复它。然后它自己尝试再次运行测试,看看我是否确实修复了这个 bug。所以我们可以批准那个执行工具的请求。然后我们发现,哦,测试现在通过了。然后在这里,模型在做一些其他智能的事情。一般来说,当你在代码中修复一个 bug 时,你想确保你刚写的用来捕获该 bug 的测试确实通过了。
[32:00] 但然后,你应该运行其余的测试,确保你没有破坏代码库中的其他东西。所以然后智能体去做了其他类似的有用事情。然后,好了,现在智能体完成了思考和工作,它做了更改来运行测试,并且它给了我一个它做了什么的总结。我甚至可以说类似提交更改的话,它会为我创建一个 git 提交,附带一些有用的提交信息等等。是的,我们可以看到这些东西实际上训练得相当好。
[32:31] 它学会了不应该只是编造提交信息。你想遵循仓库的风格。所以它实际上在查看,好的,它在查看 git status 来看什么处于提交暂存状态以及什么被更改了。它在查看 git diff 来确保更改的是它做的事情,而不是我过去做的其他更改。然后它在查看 git log,比如最近的提交信息长什么样,这样它可以尝试匹配风格。然后它写了这个提交信息,实际上看起来相当合理,并且说,哦,这修复了这个 GitHub issue 等等。后面有问题。
[33:22] 是的,所以问题是,你给编码智能体的提示词以及底层大语言模型是否会影响代码的质量、代码的冗长程度、完成任务所需的时间等等。答案是绝对肯定的。目前来说,给大语言模型写好提示词与其说是科学,不如说更像一门艺术。而且,底层模型也在不断进步。所以情况可能是这样的:如果你使用两年前的模型——现在没人真的用那个了——但如果你用一个非常老的模型,也许你仍然能让它完成这样的任务,但你必须非常详细地描述该怎么做。而现在你只需要说,有个 bug,修一下,它就能自己搞明白。
[33:55] 不过是的,我认为有很多资源。我想我们在笔记里链接了一些,我们还可以链接更多。有很多关于如何有效地给这些工具写提示词来完成各类任务的资源。如何让它实现一个复杂的功能?人们遵循各种模式。对于像这样的小事,让我的东西看起来有复古感,我只说了让它看起来复古,它就去做了。但对于任何复杂的、严肃的功能,你可能需要一段非常长的提示词,长达一段又一段。或者也许你想遵循更接近业界人们做法的软件开发实践。
[34:26] 也许你想戴上产品设计师的帽子,戴上产品经理的帽子,写一整份 PRD,即产品需求文档,然后你可以把一份巨大的 Markdown 文档交给编码智能体,说,嘿,这是一份 PRD。读一下,理解它,制定一个实现计划,然后实现它。所以你可以遵循这样不同的高级模式来做更复杂的事情。是的,有问题。[听不清的问题] 是的,所以问题是,如果你在一个目录中启动编码智能体,它能看到该目录和子目录中的所有内容吗?
[35:03] 初步来说,是的。其中一些智能体还可以请求查看目录之外的内容。这个领域还相当新。市面上有很多不同的工具。而且其中一些细节因工具而异。但是的,基本上是这样,你通常会希望在 Git 仓库的层面上操作。好。所以我们看到了这个端到端的例子……根据失败的测试来修复 bug。我们也可以顺便看看,哦,它还在思考,让我们的课程网站看起来有复古感。
[35:36] 好的。对,我们不要提交并推送那个改动。不过,这些东西对创意工作其实也相当有用。如果你读了今天的讲义,部分原因也是为了试用这项技术,我们用了 Claude Code 来处理与课程网站相关的若干改动。当我们把这一讲从之前的开发环境那一讲中抽出来、变成独立的一讲时,需要做一些重构。我们就让 Claude Code 帮我们做了。它做得还不错,但并非完美。你可以看链接的 GitHub 拉取请求,看看智能体做了什么,以及我之后不得不手动修复了什么。
[36:11] 它没有把所有练习都迁移过来并放到完全正确的位置。有些练习直接消失了,我不得不把它们恢复。我们还实现了诸如……来,让我把这些改动撤销掉。我们还用 Claude Code 实现了诸如 Tufte 风格的边注之类的东西。所以,Jon 自己一行代码都没写。全都是用 Claude Code 做氛围编程。所以,对于这类创意工作,只要给智能体一个高层次的描述让它去做,然后看看结果,再给它反馈做些微调,实际上可以非常有效。
[36:44] 比如我知道现在有很多人用这些智能体做前端设计和网页设计之类的工作。好。我们正在讨论用例。我想我们还剩大约 10 分钟。所以我不会把讲义里的每一个用例都演示一遍。而且,我相信你们能想出比我们讲义里写的多得多的用例。但我会简要地过一遍。然后我想把讲座最后一点时间花在与智能体相关的更高级的话题上。所以,用编程智能体做的一些非常有用的事情包括重构之类。
[37:19] 所以正如我提到的,我们用 Claude Code 实际重构了讲义。但你也可以用这些工具来重构代码。这些智能体非常擅长代码审查。所以如果有人给你发了一个 GitHub 拉取请求,或者即使你只是想自己用,你都可以直接让智能体审查这段代码。仅仅这个程度的指导就已经能获得相当不错的反馈了。你也可以给它更具体的指导,以获得更有针对性的回复。所以,我用这些工具做过很多的一件事,而且实际上用它们抓到过一些非常隐蔽的缺陷,就是在做性能优化或其他类型的重构时,我希望改动前后的代码在语义上等价。
[37:56] 不过没错,我当时是在做一些风格上的改动或者性能优化之类的事情。而编码智能体在推理代码以及推理代码之间的差异方面其实相当不错。它能够指出,哦,你做了这个改动。而且,哦,在这个特定情况下,行为实际上是不一样的。然后我就可以去修复那个 bug。所以对代码审查真的很有用。这些智能体也非常擅长理解代码库。所以我知道……我知道这里至少有一大部分听众是刚接触计算机科学的。你们现在可能在做比较小的代码仓库。但如果你想为 GitHub 上的开源项目做贡献,或者深入一个更大的代码库,比如你在做一个 UROP,正在处理你实验室的代码库,这些智能体非常擅长浏览代码库、理解它们并向你解释。
[38:36] 所以你甚至可以进入一个全新的代码库,直接说,给我解释一下这个代码库。或者如果你在找特定的东西,也许你刚开始一个 UROP,你的 UROP 导师说,哦,去做这个功能。光是知道要修改哪些文件就可能需要大量的思考和理解,对吧?但这些编码智能体非常擅长帮你做到这一点。我再快速演示一个,我把它和我们之前的一些讲座联系起来。因为编码智能体可以调用工具,你可以把它们当作一种 shell 来用。所以我最近实际上就在用这个,因为记住 shell 命令和它们的各种标志之类的东西可能很有挑战性。
[39:11] 相反,你可以直接用自然语言或编码智能体来解释。怎么做?你想让它做什么,比如,假设我想用一个命令来找到这个目录下所有带有重命名导入的 Python 文件,我可以说用 ag,这是一个命令行工具,来找到所有做重命名导入的 Python 文件。所以这就像 from something import something as something,或者只是 import something as something,在 Python 里,这只是我在找的一种特定模式,但自己打出来要难得多,比如记住 ag 有这个 --python 标志,或者正则表达式到底该怎么格式化之类的,所以你只需和你的编码智能体对话,让它像你的 shell 一样工作。
[39:54] 你还可以给它追加后续指令,比如忽略 lib 目录。这样它就会同时搜索你写的代码和这个库目录里的代码。所以也许我只想搜索自己写的代码,你就可以这样给它追加后续指令。它会生成正确的 shell 命令,替你运行,甚至帮你总结结果。所以我觉得这是个相当不错的用例。对,有问题吗?好,问题是,在学校作业中使用这些工具会不会惹上麻烦?请询问你的老师,看看使用这些工具是否可行。我觉得这因课程而异。
[40:25] 我见过一些课程禁止使用任何 AI 编程工具。比如不允许 AI 自动补全,不允许内联聊天。所以连我们上一讲提到的那些较简单的功能都不行。当然也不允许使用编程智能体。然后我也见过其他一些课程,态度是:想用什么工具就用什么工具。对。所以请遵守规定,不要在作业中作弊。我还觉得,像 Anthropic 和 OpenAI 等一些公司,已经为课业用途推出了专门的软件功能或模式。
[40:57] 你可以使用一种模式,在这种模式下它不会直接给你答案,而是更像助教一样行事。或者,如果你只是想优先确保自己理解,可以向工具请求提示。即使它没有专门为此设计的模式,你也可以说:代码里有个 bug,别告诉我 bug 是什么,但给我指个方向。它们在这方面还算凑合能用。所以,你可以把它们当作助教的替代品。很好。最后,我想我们不会深入讨论这一点,但你可以用编程智能体来做所谓的氛围编程。这些东西是相当强大的工具,你实际上可以完整实现整个项目,从头到尾不用自己写一个字符的代码,只需用英语和你的语言模型聊天就行。
[41:37] 我觉得让网站看起来有复古风格就是一个小例子。好,关于编程智能体的这些用例、氛围编程,有什么问题吗?很好。那么现在我们来稍微谈谈这些智能体更高级的功能。我还会把其中一些内容与我们之前讨论的这些工具底层工作原理联系起来。也许我会把这些写在黑板上。
[42:13] 这类软件大多都有几个功能。从高级功能中最简单的开始。你可能会经常发现自己向这些智能体输入相同的命令。比如,你在做某个特定项目,并且对于智能体如何审查别人发给你项目的拉取请求,你有特定的要求。
[42:47] 因为比如说,这里有项目指南,你的审查应该关注代码质量,也应该关注正确性,但不用担心安全性,因为安全性在这个项目里不太相关。你可能会……发现自己经常把这段提示词作为输入交给你的语言模型或编码智能体。所以通常这些东西都有某种功能,让你可以保存一整段提示词,就是一大段文本,之后更方便地调用它,这样你就不必在某个地方放一个文本文件,再从里面复制粘贴到编码智能体里。
[43:26] 另一个我觉得非常有用的功能是并行智能体。在一些编码智能体框架中,这其实是内置功能。在其他情况下,这只是一个你可以遵循的工作流程。但正如我们所见,即使在今天讲过的例子里,即使是相当简单的事情,这些模型也可能需要几分钟来思考和工作。如果你给它们更高级的功能去处理,我见过模型跑去做一个代码库,一干就是20分钟、30分钟。你不想只是坐在那里盯着它干活。你可以做的是直接启动多个智能体副本。
[43:58] 比如你可能有一个想修的 bug,还有一个想写的功能,另外还有一个想写的功能。你可以并行地做所有这些事。所以即使在这节课里,我实际上也同时运行了不同的智能体。在这个例子里,它们是在不同的代码仓库上。我让一个 Claude 去做课程网站的复古风格,同时让另一个 Claude 去修我代码库里的一个 bug。但当你想要多个智能体在同一个代码库上运行时,有一个特别有用的工具,叫做 git worktree。所以如果你只是在同一个目录里启动多个 Claude,比如它们都在编辑文件,它们就会互相冲突,对吧?
[44:33] 所以非常方便的是,你拥有代码库完全隔离的副本。如果你在 Git 仓库里工作,Git Worktree 就是一个非常有用的工具。Git Worktree 让你可以创建独立的检出。所以我就像这样,git worktree。我不会详细解释整个命令,你可以用 git help worktree 查看细节。这个命令做的是为我创建一个新目录,并在当前所在的位置创建一个新分支。所以现在我可以在这里启动一个 Claude,然后到这里来。
[45:05] 这里还有我仓库的另一份完整副本。我可以在那里启动另一个 Claude,给它们分配不同的任务。我可以把这些改动提交到各自的分支,等完成后再把它们全部合并到一起。所以并行智能体和 git worktree 配合得非常好。还有另一件事,我记得之前有人问过,就是有没有标准化的协议来把智能体框架和 LLM 连接起来?与此相关,有一个叫 Model Context Protocol 的标准化协议,用于把智能体框架与工具连接起来。
[45:43] 所以有各种开源的东西,也有一些托管的东西,比如你可以把你的编码智能体连接到 Notion。这样你就可以直接跟 Claude 说,哦,这是一个 Notion 文档的 URL,帮我填一下,读一下里面的内容,改进我在里面写的实现计划,然后在我的代码库里实现这个功能。所以你可以通过 Notion MCP 连接器读写 Notion。所以这只是一种把你的编码智能体框架连接到其他工具的方式。然后还有一堆功能。
[46:25] 在编码智能体中,这些功能与上下文管理有关。回想一下我们提到过,底层 AI 模型的上下文窗口是有限的。通常模型有某个固定的窗口。可能是,哦,这个模型只支持最多 100,000 个 token 的输入之类的。而在实践中,如果你给模型大量输入,即使技术上在上下文窗口之内,对模型来说处理所有这些输入也是一项更困难的任务。所以就会出现上下文被污染之类的情况。
[46:57] 所以如果你能有效地管理给底层 AI 模型的输入,它通常表现会更好。这些智能体框架支持的一些功能包括清除上下文窗口这类基础操作。通常我们聊天时,只是往消息历史里不断追加消息。而在编码智能体中,我们所有的工具调用以及这些工具调用产生的输出也会被追加到消息历史中,也就是上下文。所以如果我们在编码智能体里做一项任务,然后想做一件与之前无关的事情,你其实可以终止这个智能体再重新启动它。
[47:34] 或者它们通常有一个命令可以清空上下文窗口,这样你就能从头开始。这些工具通常还能做的另一件方便的事是回退对话。假设你正在和智能体聊天,然后你让它做某件事,之后又改变了主意。处理这种情况的一种方式是打断智能体,然后给它一条后续指令。但这样一来,自你上一条消息以来它做的所有事情现在都在上下文窗口里了,把这些内容留在那里既令人困惑又没什么用。因此,这些智能体框架通常有一个命令,可以用来把消息从消息历史末尾弹出,直接回到过去。
[48:12] 所以就像是假装那些事情从未发生过,把它们从上下文中删除。再说一次,这有助于你保持较小的上下文窗口,让 LLM 更专注。关于这些还有什么问题吗?这些工具支持的另一个与上下文管理相关的功能是压缩。那么,如果你的底层 AI 模型上下文窗口是固定的,你如何支持无限长度的对话呢?通常,无论是在 ChatGPT 这样的对话式聊天工具中,还是在 Claude Code 这样的编程智能体框架中,其工作方式都是:一旦你的对话历史变得非常长,智能体会自动或按需压缩历史前缀中的内容,从而缩小上下文窗口。
[49:01] 所以,如果其中一些会话是打开的,比如在 Claude Code 中,我甚至可以调用斜杠 compact 命令。它实际上就是用 LLM 把我的消息历史总结成一段更短的消息历史,并用这个摘要替换我上下文的前缀。这样我就能继续和我的智能体对话。通常,这些工具会自动执行此操作,但如果你发现已经和智能体聊了很久,上下文变得太长,导致它无法有效执行,你可以手动调用这个命令。
[49:34] 比如我们可以看到,这里会实际展示压缩后的上下文是什么样子。我们现在不会详细看这个,但我建议你试着运行一个编程智能体,来回交流几次,让它调用一些工具,然后压缩历史记录,看看 LLM 生成的摘要是什么样的。接下来与上下文管理相关的是叫做 LLMs.txt 的东西。编程智能体中使用的底层 AI 模型是固定的,或者说是在时间上冻结的,它们会不时更新。
[50:10] 但在任何一个给定时刻,模型都是冻结的,它是在模型训练时存在的某些数据上训练的,但不一定掌握自那以后发生的所有事情的最新信息。所以一般来说,语言模型都有一个叫做知识截止点的东西。然后,如果你让你的编程智能体,比如说,用一个昨天刚发布的库来实现某个功能,模型对这个库将一无所知。那么你该如何处理这种情况呢?处理的办法是,在推理时,你在模型上下文中向模型提供关于这个库的信息。
[50:44] 所以有一个……这些语言模型通常可以从互联网上获取代码。所以我现在手头没有具体的库,但我可以说,获取 missing.csail.mit.edu 并给我一份课程安排摘要。所以你可以这样说,这是我想用的某个库的 URL,读一下这个库的文档,然后写一些实现 xyz 的代码。
[51:15] 它就会抓取这个网站。在这个编程智能体所拥有的工具集合中,有一个网页抓取工具,它会把这些内容放进自己的上下文窗口,现在它就能基于这些信息做事了。而 llms.txt 的用武之地在于,llms.txt 是一个为 LLM 格式化信息的提议标准。通常,当这些编程智能体从网站抓取内容时——这可以通过多种方式设置——但如果你说抓取原始 HTML 并把所有这些都交给智能体和底层 LLM,HTML 是相当冗长的,里面有一堆对模型没什么用的信息。
[51:50] 在这种情况下,所有的样式信息、我们的分析数据之类的东西,都与这个模型想要做的事情无关。所以 LLMs.txt 是一个标准,用于提供旨在推理时被 LLM 消费的纯文本文件。很多库,尤其是那些更偏 AI 相关的或更有前瞻性的库,都会发布一个 LLMs.txt。所以我们可以在这里看一个例子。比如这是 pydantic ai 的 llms.txt,我们可以看到,这比模型去抓取这个页面的 HTML 内容要可用得多。
[52:28] 特别是,它要紧凑得多,所以你使用的上下文更少,模型也能表现得更好。还有另一个算是标准的东西,叫做 agents.md。这是一个你放在 Git 仓库或项目中的文件,其设置方式是,每当你启动编程智能体时,它都会读取这个文件的全部内容。
[53:02] 对于 Claude 来说,这是一个 claude.md 文件。如果你想看一个示例,我们在课程讲义或本课程的代码仓库里其实就有一个。这是我们的 agent.md 文件。所以,如果你发现自己需要给编码智能体提供一些指令,告诉它如何在你的代码仓库里完成一些基本操作,比如当你启动这些智能体时,它们的上下文中什么都没有,如果上下文一开始完全是空的,它对你们的代码仓库一无所知,对吧?所以,如果你们有一些特定的实践规范,或者类似“哦,运行这个命令来跑类型检查器,运行这个命令来跑单元测试”等等。
[53:38] 这样你就不必每次启动编码智能体时都把这些信息提供给它。这就是,你可以……这也和可复用提示词有点关系,但它的做法是,每次你启动编码智能体时,直接把这些内容塞进上下文的前缀里。所以,对于你长期使用编码智能体来开发的代码仓库来说,这些东西非常有用。这些智能体运行框架在上下文管理方面还支持另一个概念,叫做技能。
[54:09] 所以像 agent.md 这样的东西是最先出现的,而技能几乎可以说是它的后续发展。人们发现的一件事是,把一大堆信息都塞进这里非常有用,这样每当你启动编码智能体时,它就能了解关于你项目所需的一切。但这里也有一个缺点。如果你有一个非常大的 Agents.md,那么在你还没开始和底层大语言模型对话之前,它就已经占用了你上下文窗口的很大一部分。所以人们发现,在这些 Agent.md 文件里,包含了关于一大堆不同主题的信息,但在任何给定时刻,编码智能体可能并不需要所有这些信息都出现在它的上下文中。
[54:43] 所以有一个很巧妙的技巧,就是增加一层间接引用。这样你就不必在 Agents.md 里预先提供所有信息,而是可以说,这里列出了你可以查看的不同内容的概览。比如,关于我们的开发实践和代码质量标准,请看这个文档;关于如何运行测试,请看这个文档;关于你应该如何编写文档,请看这个文档,等等。这样你预先提供给智能体的信息量就很小。然后,基本上是在一个单独的文件里,智能体可以打开、查看,并按需加载到它的上下文窗口中,大致相当于一次工具调用,你就拥有了关于每个主题的所有后续信息。
[55:28] 我认为在编码智能体的上下文管理方面,我想讨论的最后一个主题是子智能体的概念。那么问题就是,如果你有一个智能体,为什么不多用几个智能体呢?为什么你会想要这样的东西?嗯,这部分与上下文管理有关。如果你有一个编码智能体正在尝试完成某项任务,并且它能够识别出一个可以执行的子任务,那么与其在同一个编码智能体中按顺序完成所有工作,不如让它启动另一个智能体,并给它一个提示词,内容就是你想让它执行的子任务,然后它就可以去执行自己的任务,完成后将控制权交还给父智能体。
[56:02] 这样父智能体的上下文就不会被子智能体所做工作的所有细节所污染。这听起来可能有点抽象,但这些东西……你可以为这些编码智能体编写自己的子智能体,而且它们有时也被用于实现这些智能体的某些组件。例如,一些编码智能体实际上将 WebFetch 工具实现为一个子智能体。因此,当父智能体想要获取网页内容时,正如我们之前提到的,将网页的整个 HTML 内容加载到语言模型的上下文窗口中可能并不是最好的主意,因为那里有太多内容了。
[56:38] 但相反,如果你能启动另一个智能体,告诉它:获取这个网站,并为我总结这方面的内容,那么它就会只筛选出相关的信息。然后这些相关信息会被放入父智能体的上下文中。这样你就能保持上下文窗口更小。这还带来了一些好处,比如这些智能体通常支持并行工具调用和并行子智能体。所以也许你有几件事想并行处理。比如,也许你写了一个子智能体,它有一个非常出色的用于代码审查的提示词。也许你还写了另一个子智能体,它有一个非常出色的用于修复类型检查相关问题的提示词。
[57:11] 也许你在开发一个功能,然后你并行启动这两个子智能体。例如,Claude Code 就可以做到这一点。然后它们会同时执行各自的任务。当它们都完成后,父智能体可以继续。所以这也能加快你的速度。是的,我只是讲解了这些内容,而没有对每一个进行演示。我认为在讲义中我们有几个更具体的例子。但在高层面上,我们想让你熟悉这些概念,然后你可以自己学习这些内容,或者跟随我们的练习,这些练习会带你逐一实践其中许多内容,从而真正学会如何在实践中使用它们。
[57:50] 那么,关于编码智能体的这些更高级功能,有什么问题吗?是的,问题是当语言模型开始进行压缩时,一般建议是让它执行压缩,还是开启一个新会话?我认为这是众多更像艺术而非科学的事情之一。我认为如果你在沿着同一条线索继续,比如生成压缩上下文的 LLM 摘要就相当不错。
[58:26] 所以如果你还在做同一件事,就继续做下去。如果你在做不相关的事情,那就清除上下文,而不是保留一堆无用的上下文,即使它是摘要。好。然后在结束之前作为最后一点,我想说这些工具确实非常强大,但 AI 并不是魔法,AI 工具也会犯错。我认为它们在一定程度上揭开了 LLM 的神秘面纱。它们只是这些概率模型,给定一堆输入 token,就会给出接下来内容的条件概率。
[59:01] 它们就只是这样。训练出一个好的 pi theta 需要许多巧妙的技术。但没错,LLM 并不像人类那样具有智能。所以,请务必审查 AI 的任何输出,检查正确性和安全漏洞。至少在我个人的使用中,我遇到过很多情况:AI 生成的代码乍看之下很合理,但实际上是微妙地错误,而且找出 bug 要花很多时间。有时验证代码实际上比你自己写出正确的代码更难。所以对于真正关键的事情,我常常会自己动手做,而不是尝试调用 AI 工具来完成。
[59:37] 还有很多其他事情可能出错,我相信你开始使用这些工具后就会体验到。比如 AI 可能会失控,钻进死胡同。它可能会试图对你进行煤气灯操纵。有时它会出错。但如果你试图告诉它错了,它就会一直坚持自己是对的。你可能会被搞得很困惑。它可能会陷入调试螺旋。它会试图修复代码、运行测试,修复代码、运行测试,结果每次都失败。它会让你的代码越来越糟。所以你确实得盯着这些东西。所以,是的,有很多潜在缺点和需要注意的事项。从高层次来看,我认为自己具备计算机科学和编程技能仍然很有价值。
[1:00:10] 我们离计算机科学家被淘汰还差得远。但与此同时,这些工具确实非常强大,值得学习。所以今天关于智能体编程的内容就到这里。我强烈建议大家做一些这方面的练习,学会在实践中使用这些强大的工具。明天见。
查看英文原始字幕
[0:01] All right, welcome to day seven of Missing Semester. Today we're going to talk about agentic coding, so continuing some of the topics on AI-powered development that we were covering in the Development Environment and Tools lecture last week. So last time in that lecture we talked about some AI-assisted coding like autocomplete and inline chat. Today we're going to be talking about something that I think is much more sophisticated and new, a different form factor than what existed, say, five years ago. which is coding agents.
[0:32] And I think right at the start of this lecture, I'm going to show you a demo of how these things work, and then we'll talk about how they work under the hood, and then talk about basics, and then more advanced topics. So that's the plan for today. So for the initial demo, I'm going to actually continue with the example we were doing during the development environment and tools lecture. So recall, I'll actually bring up the code here on the screen. So recall that we were working on this little script to...
[1:05] Oh, it's not what I wanted. One second, I need to do a little bit of setup. Okay, so recall that we were working on this script to download a page from the course website and extract all the links from it. And this is where we're at. So pretty simple little Python script. And suppose we wanted to take this script we hacked together and turn it into a slightly nicer command line program.
[1:40] Maybe we want to do things like add some argument parsing, make this a page we want to fetch the contents from, a command line argument, things like that. So we can ask a coding agent to just do this entire task for us end-to-end. And so in today's lecture, I'm going to use Claude Code, which is one particular coding agent. There are a bunch of other tools out there. We have a recommended software section at the bottom of today's lecture notes. And they're both command line coding agents, which I at least personally prefer to use at this time. There's also coding agents built into IDEs.
[2:11] So if you're using something like VS Code or Cursor, you can access roughly the same type of functionality within the IDE. And so here I've started up Claude Code. I just opened up a terminal in my directory where I have my code, and I type. Claude, and this brings up this command line interface to Claude Code. And a coding agent is basically just a conversational AI model with access to some tools that will help it write code. So if you've used something like ChatGPT before, this is like roughly ChatGPT, except the AI model can do things like read and write files and execute shell commands on your computer.
[2:50] And then sometimes these models are specially tuned to be able to be particularly effective at writing software. And so I have this dl.py script in this directory, and I can go ahead and just give natural language input to the coding agent and say something like, turn the script I wrote... here, I typoed this on purpose, I'll just leave it that way. Turn the script I wrote, dl.py, into a proper command line program, use argparse for argument parsing, and maybe I wanted to make it nicer in some other ways like some things I'm missing here Oh, I guess I have type annotations here, but I can say, like, make sure it has proper type annotations, check that it works using mypy.
[3:36] So I want to check the type annotations, make sure it passes type checking. So I can give it a command like this, and then it starts thinking. And then let me just explain its output. So it gives some textual output, like ChatGPT can say text back to you. But then it can also invoke tools. So this model decided to read the file dl.py. That's a reasonable way to get started with this task. And this is a pretty easy task. So it just updates this dl.py and in one shot writes all the code.
[4:09] So I can go in here and see what changes it's made. As we can see, it's written some more functions. And now it has this main function that has the command line argument parsing and stuff. So it's just done the entire task I asked it to do. And it's continuing to follow my instructions. So recall that I told it to make sure it has type annotations and that the type checker says that everything is good. And so here it's trying to invoke the next command. So it's using the shell and invoking mypy and giving it as an argument the path to the script.
[4:41] And one thing we noticed here, when it was editing files, it didn't ask me for permissions. It just went ahead and edited stuff. That's because I have my coding agent configured in this way. Because I find that if you need to, if it's asking for permissions for every single little change it makes to files, it's kind of annoying. I want it to be able to operate more autonomously. And generally, I track everything in Git. So I can let the coding agent make a whole bunch of changes and then review them at once later. But for other tools that the coding agent wants to invoke, like bash, I want to make sure I'm actually checking every single command that it runs and making sure that it's safe.
[5:14] We won't get into the details of agent security today, but there are some security concerns if you let these things run fully autonomously. And they might do things like invoke a command that you don't want it to invoke. It might say delete a file that you care about or much more dangerous things than that. At least I personally recommend that you run them in this mode where you have to approve individual tool calls that can do things like mutate your computer state. So I've approved that tool call. The agent harness ran the tool. It gave the results back to the AI model. The model said that everything looks good. And then it's deciding to continue along and wants to run the script and make sure it seems to be working.
[5:50] So it's doing some more things here. And there we go. It has completed the task I asked it to complete. So that's what an AI... Yeah, question? Yeah, so the question is, like, this is using some hosted AI service in the background. Can you limit how much money you're spending? The answer is, yeah, yes, you can. It depends on the tool exactly what the configuration knobs are.
[6:22] Generally, you can select which AI model you want to use and then like higher capacity models, which might be better at solving harder tasks, will be more expensive. And then smaller and faster models will be cheaper. So that's one control you have. And certain services actually slightly limit spend. Like you can say, I want to spend at most, whatever, $10 this month. Yeah. So the question is, do I limit spending? At least personally, I don't. I keep an eye on the usage. So in Claude Code, for example, there's a slash cost command.
[6:53] And this thing we've done so far has cost us about 19 cents. So I'll keep an eye on this and make sure things are not getting too unreasonable. I think if you're a student, one decent way to get started might be go for one of the subscription plans that's $20 a month, and then they have some amount of usage included with that. And then if you're going above that amount of usage and think about how you're using AI. I know that In a lot of companies, people are finding these tools are loved by engineers and increase productivity enough that compared to the cost of the engineer's time, the tool cost is relatively low.
[7:26] And so I know a lot of people who are spending a lot, like several hundred dollars a month in AI usage, but it's fine for their purposes. Yeah, question. Yeah. Coding agents cost money and they also require internet to first approximation, yes. You can also use coding agents that run locally and use models that run on your own laptop. There are trade-offs there.
[7:57] You have to use much lower capacity models unless you have... You basically have to use much lower capacity models than the one I'm using in this demo, for example. And then they might work okay for simple tasks, but for harder things, they don't really work that well. And I think for practically today, it makes sense to use one of the hosted models and pay for it. Yeah, the question is about subscription versus pay once. I think they're all subscription because inference costs just depend on usage. And then the pricing models vary and you can Google them and find it.
[8:30] I think we don't want to spend too much time in this lecture talking about pricing. We're going to talk about what are coding agents conceptually, how to use them effectively, and so on. Cool. So in this example, I've shown you a very high level overview of it. demonstration of using a coding agent, show it how we can talk to the tool. Here it did everything correctly. So I just did this one shot thing. I gave it an instruction. It did a bunch of stuff, including calling tools, looking at the results, calling more tools, depending on what happened. And eventually just coming back to me and saying, okay, I did the thing you asked for, and the end.
[9:01] You can also do multi-turn interactions with these things. I can actually remove the type annotations, and it will keep going. It has the context from the rest of the previous conversation, and I can send a follow-up messages like this. And now this is a kind of silly thing. It's kind of making our program worse. But it did the thing. More practically, you would iterate on the thing you're working on. If you were working on a feature where you didn't exactly know what the spec of the feature should be, you might give it some rough description. It does a thing. Then you judge the results. It's like, oh, actually, I want you to tweak it in this way or that way. And you can use these tools in this iterative manner.
[9:37] Another thing you can do is if the coding agent goes wrong, either if it does the wrong thing and gets confused, or if you see it going off the rails and it's kind of thinking a lot or calling tools in a nonsensical way, you can even interrupt it, like with Control-C, for example, in this tool I'm using, and then give it a follow-up message. So if it was calling, say, the wrong tool, like maybe I had mypy installed in some funny location and it couldn't find it, I could just interrupt it and be like, actually, mypy is here. Call it this way. for example. So any questions about that?
[10:10] Yeah. So the question is, can you hard code things like don't touch this file or don't touch this part of the file? In the particular tool I'm using, I think you can't hard code not being able to touch a particular part of a file. The way I would approach something like that practically, I think it's just like keep track of everything in Git, see what it changes. like if it changes a thing you didn't want to change, revert it. You can also give it natural language instructions. Like you can say, remove all the type annotations except for in this function or like don't change this chunk of the code.
[10:42] And it generally does a decent job of respecting those instructions. Yeah. So the question is, do I have experience with test-driven development using the agent? And the answer is yes. I think we will get to that a little bit bit later in the lecture. TLDR is a pretty good way to use these tools. Either write the test yourself and then tell the agent to do the thing, like implement the feature such that it passes the tests, or you can even use the coding agent to help you write the test to begin with, and then you can ask it to implement the feature that passes the tests.
[11:15] These things work really well with feedback loops. So if you can set them up so that it's like, okay, maybe you have some code that doesn't compile, or some code that doesn't type check, or something like that, you can say, here's the error, here's what you can run to reproduce the error, and keep working at it until you fix it. And that works well with test-driven development as well. Okay, so we're going to talk a lot more about practically how to use these tools in different use cases and stuff. But one topic I wanted to cover today was talking a little bit about how the underlying AI models and also how these agent harnesses work.
[11:51] Now, this is like a very deep area of research. I'm obviously not going to cover this topic in its entirety in whatever. the 10 minutes I'm planning on spending on it. I could spend an entire semester going into in-depth on the topic, but I think I'm going to try to cover it at just the right level of abstraction such that you learn the aspects of these tools you need in order to be able to use them effectively. So I'm not going to teach you how to re-implement one of these things on your own, but I will try to share a little bit about how they work so that you understand what they're good at and their limitations and why people have implemented certain features and agent harnesses and topics like that.
[12:31] So AI coding agents consist of roughly, I'd break them down into two main parts. There's an underlying language model, and then wrapping that language model is an agent harness. And so I don't know how much background everybody has on language models. Maybe raise your hand if you've used ChatGPT or any kind of AI language model before. Okay, great. Okay, so most of the hands went up. Language models, or sometimes called large language models, can be thought of as modeling a probability distribution of completion strings.
[13:08] Let's call those y. Modeling completion strings as a function of prompt strings, which we'll call x. So completions, completions y, and prompts x. So it's just this. conditional generative model that will tell us, given a particular input, a sequence of strings, what is the probability distribution over output strings that correspond to that sequence of inputs.
[13:40] And then when we use these models, and then so we can say like model this as a distribution, I'll write it as pi theta, this parameterized distribution of completions y given prompts x. And then when we use these things, we sample from this distribution, You can sample strings y hat from this pi theta given inputs. And that is it. That's all an AI language model is.
[14:11] And there's a lot of work that goes into finding a good probability distribution that does something useful. But then in practice, how you use them is you give them a bunch of text or strings or tokens as input. And then they give you back a bunch of text as output. And then everything else is built on top of that core primitive. One other detail to note here is that in practice these things have a limited context window. And basically that means that the length of this string and the length of the string have bounds.
[14:51] All right, so then building on top of this... Like how does something like ChatGPT work? Like how does conversational chat work? I think you could probably imagine how you could use this for single turn chat. Like you give it a question here and you get an answer here. Like how would you do follow-up questions? Any ideas? If you wanted multi-turn chat. So the way these things work is they use turn markers and you give your entire conversation history as the input to the model and it gives you the next thing, the model's output.
[15:33] as the output from the probability distribution. So you might give it as an input something like the user says, I don't know, like can't write out long questions on the blackboard, but like what is one plus one? And then the assistant might say, so in the first query, when you sample from the probability distribution, like this is your first X, and then this thing is your first Y hat. And then if you want to ask a follow-up question, So I'm writing a dependent query here on purpose.
[16:10] Like what if you replaced the ones with twos? So this query on its own is not super meaningful. You need the context to be able to answer this thing correctly. And so the way a conversational chat app handles something like this is that it will sample Oh. from the large language model again with this whole thing as being the prompt with some special encoding of these things as turn markers.
[16:43] So the user said this, the language model output was this, now the user said this, and that will generate the next thing. So this will be your next output. Okay, so we've gone from this primitive of large language models, which you can think of as a probability distribution to how you use them for multi-turn chat. And next we will talk about how you build something like an agent harness, building on top of that idea.
[17:18] So any questions about that so far? Cool. So one more idea you can add is, like here we added turn markers. Now we can add some special interpretation of the outputs from the LLM. And we can say the model can output things like assistant and then some text. But we can also have outputs that are like call a tool.
[17:52] So we will train a language model such that it can output requests to do things on your machine. So it might be... So an example of this is something like the things we gave up here. An example of a tool call might be a request to run, I don't know, like mypy, dl.py. And then we will have some code that wraps the language model, kind of like in our conversational chat app, we had this thing which would build our follow-up inputs by concatenating together the entire message history, we will have an agent harness, also sometimes called scaffolding, which will interpret these outputs.
[18:43] So this is a y-hat from the LLM. It'll interpret these outputs. It will actually execute them on the machine and then feed them back into the language model as a subsequent input. So it will do the thing. And then maybe there's all this textual output from the mypy tool. Like it says like checking dl.py, blah, blah. Maybe it says like error line one, expected int, blah, blah. And you will take all this output and actually just stick it in as another input to the LLM.
[19:16] So this forms a subsequent component of the X that you'll feed into the LLM. Yeah, question? So the question is, what exactly is the agent harness? It does a number of things. It will implement the aspect of the code, which runs calls to the LLM in a loop, takes the outputs.
[19:48] If there are tool calls, it will actually dispatch the tool call. It will actually execute this thing on your machine. It will get the text that this thing produces and feed it back to the language model and have it produce another output. And so... Yeah, so those are the two main things it does. So roughly, agent harnesses will take in your user input, then in a for loop, call the agent. If it requests a tool call, it will execute the tool, feed the input back into the language model, have it generate another completion.
[20:19] If it's another tool call, it will execute the tool, feed back the results to the language model and produce another completion and so on until the language model outputs textual output for the user, at which point... it will say like okay I'm done, control's back to the user and then you can proceed and so you'll have tool implementations like you might have in your coding agent tools like bash so you can have your agent execute bash scripts you might have things like file edit like file read write and so on and together those combination of tools lets the underlying AI model actually make changes to things on your machine and like develop code in some intelligent way.
[21:05] Doing things like being able to run tests, see what happens when you run the test, and take that into account when deciding what actions to take in the future. Oh, is there a question in the back? Okay. So these are the main primitives. Like there's a language model that's modeling a probability distribution over strings or tokens at the heart. And then there's this. agent harness that will call the LLM in a loop, do repeated samplings, and also dispatch requests to execute tools, actually run them and feed the results back to the language model to drive things forward.
[21:41] Usually when you type in a query, it'll run a bunch of tools and kind of under the hood, it's sampling from this language model many, many times. Yeah, question? Yeah, so the question is, is there a well-understood interface between the agent harness and the underlying language model so that you can have them interop? And yes, there are some commonly used interfaces. It's not perfectly standardized and everybody's using the same thing, but there's a good amount of interop these days.
[22:17] So for example, there are open source agent harnesses like OpenCode, and then you can have them work with different underlying language models. They can work with the Anthropic models and they can work with the OpenAI models and so on. Yeah, question. Yeah, so you're pointing out that the language model is this probabilistic thing and the agent harness is probably like the code you write in the harness itself is deterministic, like how do these things relate?
[22:57] So they relate through sampling. When the agent harness calls to the LLM, it's sampling an output string and then doing things based on that. So you will call into the language model, it'll output something like run this command, and then you will run the command and continue. Related to the non-determinism, this language model is trained in a smart way, but it's not intelligent in the same way that people are intelligent. It will sometimes output things that are not quite right. So it'll call commands that don't actually exist on your computer or use these file read-write tools. to write incorrect code or things like that.
[23:30] But in practice, these things tend to work reasonably well. And as you use them, you'll build intuition for what they're good at and what they're not yet good enough to do. Yeah. So we already did. Yeah. So now that we've talked about how these things work, We can go back and look at this example again and understand it from the lens of how things work under the hood.
[24:01] So here I've booted up my agent harness. In this particular case, I'm using the Claude Code agent harness, which is developed by Anthropic. And under the hood, it's using Anthropic's Opus 4.5 large language model. I gave it the first query here. So that's like the initial user query up in the top right. The model here outputted some intermediate text. and then decided to call a tool. So this LLM called the read file tool with an argument dl.py. That went to the agent harness.
[24:32] It interpreted that language model output as like, oh, the language model is asking to call a tool. It actually executed the file read operation on my file system, produced that data which corresponds to the contents of the file, and then fed that back in as additional input to the language model. And so again, think about the example in the top right. This is adding more and more stuff into that X. Like we kind of have this X that we start off with, and we're appending more and more things to this conversation history up until the most recent message or data we're passing into the LM.
[25:06] in this example. So like the last thing sent to the LLM right here will be the contents of that file. So then the model, we run inference, we sample from the model, and now the model decides to output some more text. It's giving me an update on what's going on. And it also outputs a request to call another tool. This time it's the file write tool. And in this particular agent harness, and I think is fairly commonly done. the file write tool, like there are a couple different interfaces, and the model can actually supply a diff which is applied to the file.
[25:38] So again the model emits a diff, so it's just a chunk of text, and then the agent harness, in this case Claude Code, will interpret that diff and apply it to the file on my file system. So then it's done that, it's outputted some more intermediate text. Yeah there's this file update operation. has a return value which is like it's like oh the operation was successful so that was given back to the model here we run another pass of inference the model decides to call another tool this time it's calling mypy so the type checker so then the agent harness interprets that runs the type checker produces the outputs uh from the tool so this time it's like success no issues found that gets passed back in to the model we run another pass of LLM inference and so on and there's like one more tool call here before it finally emits the like I am done with my my turn, here's the final text output.
[26:27] and control goes back to the user. So like a whole bunch of LLM inference steps happening in here and a whole bunch of tool calls happening in here for a single user prompt, right? Cool, and then just a brief note on privacy while we're talking about how these things work under the hood. So as we talked about a little bit earlier, these higher capacity language models, which tend to be better at doing these complex tasks, need pretty beefy. hardware to run inference, like to sample from that probability distribution.
[27:02] And so these things run in the cloud for the most part. And when you're using these things, like unless you have some kind of special agreement with the provider, you are sending them your data. They may train on the data and things like that. So if privacy is a concern, look into the details of the terms and conditions or find some setup that works for you. All right. So let's spend the rest of this lecture talking a little bit about use cases. And I think I'll try to spend... Yeah, question. Yeah, so the question is, is Claude the LLM and the agent harness both? The answer is there are a couple different setups.
[27:36] In this case, a component to the agent harness can be thought of as running in the Cloud along with the model, which is definitely running in the Cloud here. You can also find open source agent harnesses where the harness will run entirely locally, but all the LLM inference steps will end up sending data to the Cloud for inference. Yeah, cool. Okay, so we've already given one example of how to use a coding agent. to implement a new feature, you can be pretty specific with how you give instructions to these things. You can also be open-ended and iterate from there.
[28:07] So for example, I can go into the Git repo for the class website, the missing semester website, and I'll boot up Claude and I can say something like, make it look retro. We can see what happens. This will take a minute or two. So we'll come back to this one. Another great use case for coding agents is using them them to fix issues in your code. So if you have a compiler issue, like your code doesn't compile, if you have a linter issue, we'll cover linters in more detail on Friday, but if you have a tool that checks for various types of errors in your code and it's complaining, you can ask a coding agent to fix the issue for you.
[28:42] And if you have unit tests, or if you have a bug in your code, one great way to at least take a first shot at solving that bug is to construct a test for the bug. This is a great software engineering practice in general. If there's something wrong with your code, write a test. So you can later use that as a regression test to make sure things don't go wrong in the future. And then once you have a test, you can tell the coding agent, like, hey, this test is failing, like figure out what the issue is and make the test pass. And so as a quick demo of this, it's a little bit hard to demo because like I want to work in a real code base and then you don't have context on the code base.
[29:15] But I will, so don't worry about all the details here, but but we'll try to show you at a high level how things are working. Okay. So I have a code base here, don't worry about the details, and I noticed an issue with the code. And in this particular case, I hand wrote a unit test for that issue. And so if I run this test, I'll see that the test fails. You can also use the coding agent to try to take the initial crack at writing the test.
[29:46] Like you could boot up the agent and say, hey, like when I use the code in this way, like things go wrong, can you write a test for it? But then once you have a test that you're happy with, you can boot up your coding agent. and I'm going to copy paste this command. You can boot up your coding agent and say something like, there's a bug. I wrote a test for it. You can run it with this command to reproduce the issue, fix the bug.
[30:17] And then just like a human developer would do in this case, like you'd first run the test to reproduce the bug. And that's what the agent's doing. So it's calling bash with this thing I gave it to run that particular test I just wrote. And then it looks at... the output and sees like, yes, this is indeed failing. That output on its own is not super informative or it's like on its own, not enough to fix the bug. You need to actually read and understand the code. So that's what the agent's doing next. These things are actually pretty good at traversing code bases and like understanding code, whatever understanding means for AI models.
[30:50] So we can see like, okay, it's reading files. It's here's another tool that it has. It has a search tool, which lets it search with a regex pattern for different things that match this pattern. It's reading more files, searching, reading, searching, reading, thinking. And I think in this particular case, the bug is simple enough that it should just, in one shot, fix the bug. Another thing that makes these coding agents a little bit hard to demo is that they're not the fastest things. And this is something that a lot of people are working on. Like, how do you make agents that are really good and effective at what they do, but also run faster?
[31:25] Like here we're seeing, oh, it's been like one whole minute this thing is running. But yeah, it makes demos a little bit trickier. can't fast forward this behavior because I wanted to do a live demo. But okay, now the model's saying, oh, here's what I think the bug is. Here, I'm going to fix it in this way. And then it tries to, on its own, run the test again to see, did I indeed fix the bug? So we can approve that request to execute a tool. And then we find, oh, the test passes now. And then here, the model's doing some other intelligent stuff. Generally, when you fix a bug in your code, you want to make sure that the test you just wrote to catch that bug indeed passes.
[32:00] But then, you should run the rest of the test, make sure you didn't break something else in the code base. So then the agent's going and doing other useful things like that. And then, okay, now the agent's done thinking and working, it's made the changes to run the test, and it's giving me a summary of what it did. And I can even say something like commit the changes, and it will create a git commit for me with some useful commit message and so on. Yeah, we can see these things are actually trained pretty well.
[32:31] It's learned that it should not just make up a commit message. You want to follow the style of the repository. So it's actually looking at, okay, it's looking at git status to see what stage for commit and what's changed. It's looking at a git diff to make sure what changes is the thing it did, not some other changes that I'd made in the past. And then it's looking at the git log, like what do the recent commit messages look like, so it can try to match the style. And then it writes this commit message, which actually looks pretty reasonable and says, like, oh, this fixes this GitHub issue and so on. question in the back.
[33:22] Yeah, so the question is, does the prompt you gave the coding agent and the underlying LLM affect the quality of the code, how verbose the code is, how long it takes to do the task, and so on. And the answer is absolutely, yes. It's a little bit more of an art than a science at this point to prompt LLMs well. Also, the underlying models are continually improving. So it might have been the case that if you use a model from two years ago, which nobody really uses now, but if you use a really old model, maybe you can still get it to complete a task like this, but you have to be really descriptive in how to do it. And now you can just be like, there's a bug, fix it, and it'll figure things out.
[33:55] But yeah, I think there's a lot of resources. I think we've linked some in the notes and we can link more. A lot of resources on how to effectively prompt these tools to do various classes of tasks. How do you get it to implement a sophisticated feature? And there are various patterns people follow. For silly things like this, make my thing look retro, all I said was make it look retro and it's going and doing the thing. But for any of the complex, serious feature, you might have a really long prompt that's paragraphs and paragraphs long. Or maybe you want to follow a software development practice closer to what people do in industry.
[34:26] Maybe you want to put on your product designer hat and put on your product manager hat and write a whole PRD, a product requirements doc, and then you can give a huge markdown doc to a coding agent and be like, hey, here's a PRD. Read it, understand it, make an implementation plan, and then implement it. So there are different advanced patterns you can follow like that to do more sophisticated things. Yeah, question. [inaudible question] Yeah, so the question is, if you start the coding agent in a directory, does it see everything in that directory and subdirectories?
[35:03] To first approximation, yes. Some of these agents also can request to see things outside of the directory. There's a lot of, the space is pretty new. There are a lot of different tools out there. And like some of these details vary between the tools. But yeah, basically, yes, you will want to operate at the level of a Git repository generally. Cool. So we saw this end-to-end example of... fixing a bug given a failing test. We can also just for fun see, oh this is still thinking, making our course website look retro.
[35:36] All right. Yeah, let's not commit and push that change. But yeah, these things can actually be pretty useful for creative work too. If you read the lecture notes for today, in part just to like kind of dog food this technology, we used Claude Code to work on a number of changes related to our course website. We needed to do some refactoring when we pulled this lecture out from the previous development environment lecture into its own lecture. And we just asked Claude Code to do that for us. It did a decent job, didn't get it perfect. You can see the linked GitHub pull request to see what the agent did and what I had to do manually to fix things up afterwards.
[36:11] It didn't carry over all the exercises and put them in quite the right place. Some of them just disappeared and I had to restore them. We also implemented things like... Here, let me get rid of these changes. We also implemented things like our Tufte-style sidenotes using Claude Code. So yeah, I think Jon didn't write a single line of that code himself. It was all just like vibe coding with Claude Code. And yeah, so for this kind of creative work, just telling the agent to do something with like some high level description and then just looking at the results and then giving it feedback to make tweaks can actually be super effective.
[36:44] Like I know a lot of people who do front-end design and web design and things like that using these agents these days. Cool. So we're talking through use cases. I think we have about 10 minutes left. So I'm not going to demo every single use case that we have in our notes. And also, I'm sure you can come up with many more use cases beyond what we've written in our notes. But I'll briefly talk through them. And then I want to spend the last bit of the lecture on more advanced topics related to agents. So some of the things that are very useful to do with coding agents include things like refactoring.
[37:19] So as I mentioned, we used Claude Code to actually refactor our lecture notes. but you can also use these tools to refactor code. These agents are really good at code review. So if someone sends you a GitHub pull request, or even if you just want to use it for yourself, you can just ask the agent, review this code. Just that level of guidance is already pretty good to get useful feedback. You can also give it more specific guidance to get a more tailored response. So one thing I've used these tools for a lot, and actually used them to catch some very subtle bugs, is in situations where I've done performance optimization, or other kind of refactoring where I wanted the code to be semantically equivalent before and after the change.
[37:56] But yeah, I was making like stylistic changes or performance improvements or things like that. And the coding agent's actually pretty decent at reasoning about code and reasoning about deltas between code. And it was able to point out like, oh, you made this change. And like, oh, in this particular case, the behavior is actually different. And then I could go and fix that bug. So really useful for code review. These agents are great for codebase understanding too. So I know like... I know at least a decent chunk of the audience here is just getting into computer science. You're probably working on smaller repositories right now. But if you want to contribute to an open source project on GitHub or dive into a larger code base, like say you're doing a UROP and you're working on your lab's code base, these agents are really good at navigating code bases and understanding them and explaining them to you.
[38:36] So you can even go into a fresh code base and just be like, explain this code base to me. Or if you're looking for particular things, maybe you start a UROP and your UROP mentor says, oh, go work on this feature. even to know what files to modify can take a lot of thought and understanding, right? But these coding agents are pretty good at helping you with that. Another quick one I'll demo, I'm tying this back to some of our earlier lectures. So because these coding agents can call tools, you can use them kind of sort of like a shell. And so I actually use this these days because memorizing shell commands and all their flags and stuff like that can be challenging.
[39:11] And instead, you can just explain in natural language or coding agent. how what you want it to do so like suppose I want to use a command to find all the Python files in this directory that have uh renaming imports I can say like use ag so this is a command line tool to find all Python files that do renaming imports so this is like from something import something as something or just import something as something in Python it's just a particular pattern I'm looking for but like typing this out myself is way harder like remembering that like ag has this --python flag or like how exactly to format regexes or things like that and so you just talk to your coding agent and have it behave like your shell.
[39:54] And you can give it follow-ups like ignore the lib directory. So this is searching for both code I wrote and code in this library directory. So maybe I just want to search the code I wrote. So you can give it a follow-up like that. It'll generate the right shell command, run it for you, and even summarize the results for you. So I think it's a pretty great use case. Yeah, question? Yeah, so the question is, will you get in trouble for using these tools on school stuff? please ask your instructor and see if it's okay to use these tools. I think it just varies by class.
[40:25] I've seen some classes that prohibit use of any AI coding tools. So like no AI autocomplete, no inline chat. So not even the simpler features we talked about in that previous lecture. And then obviously no coding agents. And then I've seen other classes that are like, yeah, use whatever tools you want to use. Yeah. So please follow the rules. Don't cheat on your your homework. I also think some of these companies, I think like Anthropic and OpenAI and others, have released special features or modes of their software for schoolwork.
[40:57] So you can use them in a mode where they won't give you an answer, but will act a little bit more like TAs. Or if you just want to prioritize your own understanding, you can ask the tool for a hint. Even if it doesn't have a special mode for this, you can be like, there's a bug in the code, don't tell me what the bug is, but point me in the right direction. And they kind of sort of work okay for that. So yeah, you can use them as a replacement TA. Cool. And then finally, I think we're not going to talk about this in depth, but you can use coding agents for what's called vibe coding. So these things are pretty powerful tools and you can actually implement entire projects like start to finish without writing a single character of code yourself just by like chatting with your language model in English.
[41:37] I think that make the site look retro is a small example of that. Okay, so any questions about those use cases of coding agents? vibe coding. Cool. So now let's talk a little bit about more advanced functionality of these agents. And I'm also going to tie back some of this to what we talked about earlier with how these things work under the hood. So maybe I'll write these down on the blackboard.
[42:13] A couple of features that most of these pieces of software have. Starting from the simplest of the advanced features. So one thing you might find yourself doing often is typing out the same command to these agents. Like say you're working on a particular project and you have a particular way you want the agent to do code review for pull requests people send to your project.
[42:47] Because like, oh here are project guidelines and oh your review should focus on code quality and it should also focus on correctness, but don't worry about security because security is less relevant in this project. You might... find yourself frequently giving this prompt as an input to your language model or to your coding agent. And so usually these things have some functionality where you can just save a whole prompt, just like a bunch of text, and invoke it more easily later so you don't have to have a text file somewhere where you copy-paste from it into the coding agent.
[43:26] Another feature that I think is really useful is parallel agents. In some coding agent harnesses, this is actually a built-in feature. In other cases, this is just a workflow that you can follow. But as we've seen, even in the examples we've gone through today, even fairly simple things, these models can take a couple of minutes to think and work. If you give them much more advanced features to work on, I've seen situations where the model goes off and works on a code base for 20 minutes, 30 minutes. You don't want to just sit there and stare at it while it's doing that. What you can do is actually just boot up multiple copies of agents.
[43:58] Like you might have. a bug you want to fix, and also a feature you want to write, and also another feature you want to write. And you can do all of these in parallel. So even in this lecture, I actually had different agents running. In this case, it was on different repositories. I had one Claude working on making the course website retro, while I had another Claude working on fixing a bug in my code base. But one particular tool that's really useful when you want to have multiple agents running on the same code base is something called git worktrees. So if you just boot up multiple Claudes, for example, on the same directory, and they're editing files, they're going to conflict with each other, right?
[44:33] So what's really handy is you have completely isolated copies of your code base. And if you're working in a Git repo, Git Worktree is a really useful tool for doing that. So Git Worktree lets you create separate checkouts. So I'm just gonna like Git Worktree. I'm not gonna explain this whole command in detail to you can do Git help Worktree to see the details. What this command does is it creates this new directory for me and also a new branch at the same place where I currently am here. And so now I can boot up one Claude in here and go here.
[45:05] And here's an entire other copy of my repository. And I can boot up another Claude there and I can give them different tasks to do. I can commit these things to their separate branches and I can merge them all together later when I'm done. So parallel agents and git worktrees go really well together. There's this other thing I think someone earlier was asking about, like is there standardized protocol to connect together agent harnesses and LLMs? Kind of related to that, there's a standardized protocol called Model Context Protocol, which is for connecting agent harnesses with tools.
[45:43] So there are various open source things and also some like hosted things, where you can, for example, connect your coding agent to Notion. So then you can just talk to Claude and be like, oh, here's a URL for a Notion doc. Like, fill it in, like, read the stuff in there, like, improve the implementation plan I've written in there, and then, like, implement the feature in my code base. So you can read and write to Notion, for example, with the Notion MCP connector. So this is just a way to connect your coding agent harness to other tools. And then there are a bunch of features.
[46:25] in coding agents that are related to context management. So recall that we mentioned that the underlying AI models have a limited context window. Generally, the models have some fixed window. It might be like, oh, this model only supports inputs up to 100,000 tokens or something like that. And then in practice, if you give a ton of input to a model, even if it's technically within the context window, it's just a harder task for the model to deal with all of that input. So you get these things like having a polluted or like... a polluted context.
[46:57] And so if you can effectively manage what you're giving as input to the underlying AI model, it'll generally perform better. And so some things that these agent harnesses support is basics like clearing the context window. Normally when we chat, we're just appending more and more messages to our message history. And with coding agents, we also have all our tool calls and the resulting outputs from those tool calls as getting appended to our message history, or also known as context. And so if we're working on one task in our coding agent, and then we want to do something else that's unrelated to what we were doing before, you can actually kill the agent and start it again.
[47:34] Or they generally have a command just clear the context window. So you're starting fresh. Another handy thing that these things can generally do is rewind the conversation. So say you were chatting with the agent, and then you asked it to do something, and you changed your mind. One way you could handle that is by interrupting the agent and just giving it a follow-up instruction. But then all the stuff it's done so far since your last message is in the context window now, and it's confusing and not very useful to have it there. And so instead, these agent harnesses generally have a command you can use to basically pop messages off the end of the message history and just go back in time.
[48:12] So it's like, pretend the stuff never happened, delete it from the context. And again, that'll help you keep a smaller context window to keep your LLM more focused. Any questions about these? Another feature related to context management that these things support is compaction. So how do you do things like support unlimited length conversations if your underlying AI model has a fixed context window? Generally, the way that works both in conversational chat tools like ChatGPT and in coding agent harnesses like Claude Code is that once your conversation history grows really long, the agent will either automatically or on demand compress the stuff in the prefix of the history, and that'll shrink the context window.
[49:01] And so I could even, if some of these sessions open, in Claude Code, for example, I can call the slash compact command. And that actually just uses an LLM to take my message history and summarize it into a smaller message history. And that replaces the prefix of my context with the summary. And that lets me keep conversing with my agent. Generally, these tools will do this automatically, but if you found that you've been talking with the agent for a while and the context is getting too long for it to perform effectively, you can call this manually.
[49:34] And so we can see here, for example, this will actually show us what this compacted context is. And we're not going to look at this in detail right now, but I recommend you try running a coding agent, have a little bit of back and forth, have it call some tools, and then compact the history and just look at what the LLM summary looks like. The next thing kind of related to context management is something called LLMs.txt. So the underlying AI models used in coding agents are fixed or frozen in time, and they get updated from time to time.
[50:10] But at any given moment, the model is frozen and it's trained on some data that existed at the time the model was trained, but it's not necessarily up to date on everything that's happened since. So generally, language models have something called a knowledge cutoff. And then if you ask your coding agent, for example, to implement a feature using a library that came out yesterday, the model will know nothing about that library. And so how do you handle that? And so the way you handle that is that at inference time, you give to the model in the model context information about the library.
[50:44] And so there's a... These language models can generally fetch code from the internet. And so I don't have a library in mind at the moment, but I can be like, fetch missing.csail.mit.edu and give me a summary of the class schedule. So you could be like, here's a URL for a library I want to use, read the library documentation, and then write some code that does xyz.
[51:15] And it'll fetch the website. among the set of tools that this coding agent has as a web fetch tool, and it will get that into its context window, and now it can do things based on that information. And so where llms.txt comes in is that llms.txt is a proposed standard for formatting information for LLMs. Normally when these coding agents fetch things from a website, and this can be set up in a number of ways, but if you were to say fetch the raw HTML and give all that to the agent and to the underlying LLM, HTML is pretty verbose and there's a bunch of information there that's not that useful to the model.
[51:50] In this case, all the styling information or our analytics or things like that are not things that are relevant to what this model is trying to do. And so LLMs.txt is a standard to have plain text files that are meant to be consumed by LLMs at inference time. And a lot of libraries, especially the more AI-related ones or the more forward-thinking ones, will publish an LLMs.txt. And so we can look at an example here. Like here's the pydantic ai llms.txt, and we see that this is way more usable than if the model were to fetch the contents of this page in HTML.
[52:28] And in particular, it's way more compact, so you're using less of your context, and the model's able to perform better. There's another kind of standard-ish thing called agents.md. And this is a file that you put in your Git repository or in your project where the setup is that whenever you boot up the coding agent, it will read the entire contents of this file.
[53:02] For Claude, this is a claude.md file. If you want to see an example, we actually have one in the lecture notes or in the repository for this class. Here's our agent.md file. And so if you find yourself needing to give your coding agent instructions on how to do basic things in your repository, like when you boot these things up without anything else in their context, if the context starts out entirely empty, it knows nothing about your repository, right? So if you have certain practices you follow, or it's like, oh, run this command to run the type checker, run this command to run the unit test, and so on.
[53:38] Instead of having to supply that information to the coding agent every time you boot it up, This is, you can. This is kind of related to reusable prompts too, but this is like every time you boot up your coding agent, just dump this into the prefix of the context. So these things are really useful for repositories you're working on for a long time with coding agents. Another concept that these agent harnesses support related to context management is something called skills.
[54:09] So things like agent.md came first and skills was almost a follow up to this. So one thing people found was this really helpful to dump a whole bunch of information in here so that whenever you boot up your coding agent, it knows everything it needs to about your project. But there's also a downside here. If you have a really big Agents.md, you're filling up a large chunk of your context window for your underlying LLM before you're even getting started talking to it. And so people found that in these Agent.md files, there was information about a whole bunch of different topics, but at any given time, a coding agent might not need all that information in its context.
[54:43] And so there's this neat trick to add one level of indirection. So instead of supplying all the information upfront in Agents.md, you can say like, Here's an overview of the different things you can look at. Like see this document for our development practices and like code quality standards. See this document for like how to run the tests. See this document for how you should write documentation and so on. So you have that small amount of information supplied to the agent up front. And then in basically a separate file that the agent can open up and look at and load into its context window on demand, roughly as a tool call, you have all the follow-up information on each of the topics.
[55:28] And I think the final topic I want to talk about in terms of context management in coding agents is the concept of sub-agents. And so it's like, if you have one agent, why don't you have more than one agent? And why might you want something like that? Well, it's in part related to context management. If you have a coding agent that's trying to do some task and it can identify a sub-task to do, instead of doing that all kind of in line in the same coding agent, if it can spawn up a different agent. and give it as a prompt just like the subtask you want it to do, then it can go do its thing, finish, and then return control back to the parent agent.
[56:02] And then the parent agent's context is not polluted with all the details of what the subagent did. That might sound a little bit abstract, but these things are... You can write your own subagents for these coding agents, and they're also used in implementing components of these agents sometimes. So, for example, some coding agents implement the WebFetch tool. actually as a sub-agent. So when the parent agent wants to fetch contents of a web page, and as we mentioned earlier, loading the entire HTML contents of a web page into the context window of a language model might not be the best idea because there's a lot of stuff there.
[56:38] But instead, if you can spin up another agent, tell it, fetch this website, and summarize this aspect of it for me, then it'll filter out just the relevant information. And then that relevant information will be put into the context of the parent agent. And that way you keep your context window smaller. This also enables some nice things like these agents generally have support for parallel tool calls and parallel subagents. So maybe there's several things you want to do in parallel. Like maybe you've written a subagent that has a really great prompt for doing code review. And maybe you've written another subagent that has a really great prompt for fixing issues related to type checking.
[57:11] And maybe you work on a feature and then you spin off both of these subagents in parallel. This is something that Claude Code, for example, can do. And then they'll both do their thing at the same time. And then when they're both done, the parent agent can continue. And so this can also speed you up. So yeah, I just talked through these things instead of giving demos of each of them. I think in the lecture notes we have a couple more concrete examples of these things, but at a high level we wanted to familiarize you with the concepts, and then you can either go through this on your own or follow our exercises, which will walk you through a bunch of these, to really learn how to use these in practice.
[57:50] So any questions about these more advanced features of coding agents? Yeah, so the question is when a language model starts in compaction, is the general advice to let it do compaction or open up a new session? I think this is one of the many things that's like, it's a little bit more of an art than a science. I think if you're continuing along the same thread, like the LLM summarization that produces the compacted context is pretty decent.
[58:26] So if you're still working on the same thing, like just keep going. If you're doing something unrelated, like clear the context instead of just keeping a bunch of useless context around, even if it's a summary. Cool. And then just as a final note before we wrap up, just wanted to say these tools are really powerful, but AI is not magic and AI tools can make mistakes. They're, I think, hopefully to some degree demystified LLMs a little bit. They're just these probabilistic models that, given a bunch of input tokens, give you the conditional probability on the things that come next.
[59:01] That's all they are. And there's a bunch of clever techniques that go into training a good pi theta there. But yeah, LLMs are not intelligent in the same way that humans are. And so, yeah, make sure you review any output from your AI in terms of looking for correctness and security bugs. Like, at least in my personal usage, I've had many situations where the AI generates some code that looks reasonable at first glance. And it's just subtly wrong. And it takes a lot of time to find the bug. And sometimes verifying code can actually be harder than just writing the correct code yourself. So for, like, really critical things, oftentimes I will just do the thing myself instead of even trying to call an AI tool to do the task.
[59:37] There are also many other things that can go wrong, as I'm sure you'll experience if you start using these tools. Like the AI can just like go off the rails, go down rabbit holes. It can like try to gaslight you. Sometimes it'll like be wrong. But if you try to tell it it's wrong, it'll just like keep insisting that it's right. And you can get really confused. It can get stuck in debugging spirals. So it'll be trying to fix code, run the test. It'll fix the code, run the test, and it's like fail every time. It'll make your code worse and worse. So you kind of do have to keep an eye on these things. And so, yeah, a lot of potential downsides and things to be aware of. And at a high level, I think it's still valuable to have computer science and programming skills yourself.
[1:00:10] We're far from the point where computer scientists are obsolete. But at the same time, these are really powerful tools and worth learning. So that is all I have for you today on agentic coding. And I highly recommend you go through some of the exercises on this topic to learn how to use these powerful tools in practice. See you all tomorrow.
在 Vidleaf 工作台中打开
搜索转录文本、选择行、复制带时间戳的引文、翻译。
署名
《讲座 7:智能体编程》由 Missing Semester(https://www.youtube.com/@missingsemester)创作,依据 CC BY 4.0 许可(https://creativecommons.org/licenses/by/4.0/)。源视频:https://www.youtube.com/watch?v=sTdz6PZoAnw。本页面是该视频的文本转录,添加了段落分隔和时间戳;创作者与 Vidleaf 无关联,也不为其背书。Vidleaf 添加了 AI 标点和 AI 生成的摘要;原始措辞仍可在本页面查看。
您是创作者或权利持有人吗?申请更正或删除:copyright@vidleaf.app(参见 关于这些页面)。
最后更新于