Agent
文章目录
工具收集
promptfoo.dev: Secure & reliable LLMs | Promptfoo
- Prompt 安全工具
prompt 收集
claude code 逆向工程博客
参考:
system_prompt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20SYSTEM_PROMPT = """You are a helpful coding agent that assists with programming tasks and file operations. When responding to requests: 1. Analyze what the user needs 2. Use the minimum number of tools necessary to accomplish the task 3. After using tools, provide a concise summary of what was done IMPORTANT: Once you've completed the requested task, STOP and provide your final response. Do not continue creating additional files or performing extra actions unless specifically asked. Examples of good behavior: - User: "Create a file that adds numbers" → Create ONE file, then summarize - User: "Create files for add and subtract" → Create ONLY those two files, then summarize - User: "Create math operation files" → Ask for clarification on which operations, or create a reasonable set and stop After receiving tool results: - If the task is complete, provide a final summary - Only continue with more tools if the original request is not yet fulfilled - Do not interpret successful tool execution as a request to do more Be concise and efficient. Complete the requested task and stop."""
构建 Agent 的要点
历史对话管理
历史数据的持久化
- 保存历史
- 加载历史
最大对话历史长度
因为对话历史长度的限制,需要限定 max_history_count,
messages = self.messages[max_history_count:]
ReAct tool 调用
Reason –> Action –> Observe
- Reason: 是否/使用哪些工具
- Action: 调用工具
- Observe: 观察工具调用结果
工具调用
两种调用方式
Human In the loop
获取人工授权,然后执行工具调用
自动调用
要点:
- 注意避免死循环,需要设置
最大自动调用次数
伪代码:
| |
Context Engineering
FAQ
context 污染
无效 context
通过 网络搜索获取的(召回)内容,可能和对话主题无关
- 这种内容可能会导致对话偏离主题
Content Engineering 案例
Claude Code
参考:
Claude Code 包括了三层 context (three-layout context)
第一层: Cluade.md
persistent layer
- 长期记忆
- 在初始化一个项目任务时,Cluade 会先扫描项目的重要文件(requirements.txt, README,项目结构等等),然后创建 Cluade.md
包括内容:
- projtect convention 项目风格
- commit 规范
- architecture decisions
- coding standards
- 已经你个人规定的其他要求
第二层: dynamic layer
- 根据用户的 query 命令,去获取项目中的相关内容来增强 context
比如:用户提取修复 auth bug, 它会
- 查找和认证相关的文件和代码,理解代码逻辑,查找相关 codebase,比如 middleware, config, tests
- 扩充 context
- 制定修复 bug 的方案
- 让用户确定是否采用
第三层:conversation
对话本身
- user query
- tools used
- content retrieved
Claude Code 如何压缩对话历史?
文章作者
上次更新 2025-09-24 (360d44c)