codex是如何定义prompt来约束与模型交互的输入输出格式的,具体格式是什么样的
A
Admin
如果你研究过早期 LangChain 的 ReAct Prompt,可能会以为 Codex 也是这种格式:
Thought:
...
Action:
...
Observation:
...
Thought:
...
Final Answer:
...
实际上现代 Codex 已经不是这样工作的。
它不依赖文本 Prompt 去约束输出格式,而是依赖:
- System / Developer Instructions
- Tool Schema(JSON Schema)
- Responses API 的结构化输出
来实现 Agent Loop。(OpenAI)
传统 ReAct
很多教程里的 Agent:
You have access to:
Search
Calculator
Format:
Thought:
...
Action:
Search
Action Input:
...
Observation:
...
Final Answer:
...
模型输出:
Thought: 我需要查天气
Action: Search
Action Input: 东京天气
Agent 再用正则表达式解析。
这是 2023 年的主流方案。
缺点:
- 容易输出格式错误
- 正则解析脆弱
- Tool 参数难约束
Codex 的方式
Codex 使用 Responses API。
Prompt 本身大致是:
{
"instructions": "...system prompt...",
"tools": [...],
"input": [...]
}
模型并不会输出:
Action: run_shell
Action Input: ls
而是直接输出结构化 Tool Call。(OpenAI)
类似:
{
"type": "function_call",
"name": "shell",
"arguments": {
"command": "ls -la"
}
}
Agent Harness 收到后直接执行。
Tool 是如何定义的
例如 Shell Tool:
{
"type": "function",
"name": "shell",
"description": "Run shell command",
"parameters": {
"type": "object",
"properties": {
"command": {
"type": "string"
}
}
}
}
发送给模型后:
模型知道:
我可以调用一个叫 shell 的工具
参数必须是:
{
command: string
}
所以格式约束主要来自 JSON Schema,而不是 Prompt。(OpenAI)
Codex 的 Prompt 真正长什么样
OpenAI 官方公开过它的组成。
大致结构:
[System]
你是 Codex
你是软件工程 Agent
遵守沙箱限制
优先修改代码而非解释
...
[Project Instructions]
AGENTS.md
项目规则
编码规范
[Tool Definitions]
shell(...)
read_file(...)
write_file(...)
...
[Conversation]
User:
修复登录Bug
然后模型返回:
tool_call(shell)
或者:
assistant_message
而不是 Thought / Action 文本。(OpenAI)
如果你做数据分析 Agent
我建议不要学老 ReAct:
Thought:
我要查询数据库
Action:
query_db
Input:
...
而是学 Codex:
Tool
{
"name": "query_db",
"parameters": {
"type": "object",
"properties": {
"sql": {
"type": "string"
}
}
}
}
System Prompt
你是数据分析助手。
工作原则:
1. 先理解问题
2. 不确定表结构时先查看Schema
3. 优先调用query_db
4. 给出分析结论
5. 不要编造数据
Agent Loop
while True:
response = llm(messages, tools)
if response.tool_call:
result = execute_tool(...)
messages.append(result)
else:
return response
这其实已经是 Codex 的核心模式了。
从我看过的 Codex 架构来看,最值得借鉴的不是某个神秘 Prompt,而是这三个东西:
Agent =
System Prompt
+ Tool Schema
+ Agent Loop
其中:
- Prompt 负责行为约束
- Schema 负责格式约束
- Agent Loop 负责执行
很多人把精力放在 Prompt Engineering 上,但在现代 Agent 里,Tool Schema 对输出格式的约束作用往往比 Prompt 更大。Codex 就是典型例子。(OpenAI)