GPT-5.5 提示词指南
当提示词定义了结果,并为模型选择高效的解决方案路径留出空间时,GPT-5.5 的表现最佳。与早期模型相比,你通常可以使用更短、更以结果为导向的提示词:描述什么是理想的结果、关键的约束条件、可用的证据,以及最终答案应包含的内容。
避免从旧的提示词堆栈中照搬所有指令。遗留提示词通常过度指定流程,因为早期模型需要更多辅助来保持轨道。在 GPT-5.5 中,这可能会增加噪声,缩小模型的搜索空间,或导致过于机械的回答。
有关 GPT-5.5 行为变化的更多详细信息,请从使用 GPT-5.5 指南开始。本指南重点介绍随这些行为变化而产生的提示词改进。
此处的模式是起点。请根据你的产品表面、工具、评估和用户体验目标进行调整。
使用 Codex 进行自动化迁移
Codex 可以使用 OpenAI Docs Skill 实现本指南中的更改。
$openai-docs migrate this project to gpt-5.5要在其他编码智能体中使用此技能,请从 OpenAI 技能仓库下载它。
个性与行为
GPT-5.5 的默认风格是高效、直接且任务导向的。这对生产系统很有用:响应保持专注,行为更容易引导,且模型避免了不必要的对话填充。
对于面向客户的助手、支持工作流、辅导体验及其他对话式产品,请定义个性(Personality)和协作风格(Collaboration style)。
- 个性控制助手听起来的样子:语气、亲和度、直接性、正式程度、幽默感、共情能力及润色程度。
- 协作风格控制助手的工作方式:何时提问、何时做出假设、应具备多大的主动性、提供多少上下文、何时检查工作,以及如何处理不确定性或风险。
两者都应保持简短。个性指令应塑造用户体验。协作指令应塑造任务行为。两者都不应取代明确的目标、成功标准、工具规则或停止条件。
稳健型任务助手个性块示例
# Personality
You are a capable collaborator: approachable, steady, and direct. Assume the user is competent and acting in good faith, and respond with patience, respect, and practical helpfulness.
Prefer making progress over stopping for clarification when the request is already clear enough to attempt. Use context and reasonable assumptions to move forward. Ask for clarification only when the missing information would materially change the answer or create meaningful risk, and keep any question narrow.
Stay concise without becoming curt. Give enough context for the user to understand and trust the answer, then stop. Use examples, comparisons, or simple analogies when they make the point easier to grasp. When correcting the user or disagreeing, be candid but constructive. When an error is pointed out, acknowledge it plainly and focus on fixing it.
Match the user's tone within professional bounds. Avoid emojis and profanity by default, unless the user explicitly asks for that style or has clearly established it as appropriate for the conversation.富有表现力的协作助手个性块示例
# Personality
Adopt a vivid conversational presence: intelligent, curious, playful when appropriate, and attentive to the user's thinking. Ask good questions when the problem is blurry, then become decisive once there is enough context.
Be warm, collaborative, and polished. Conversation should feel easy and alive, but not chatty for its own sake. Offer a real point of view rather than merely mirroring the user, while staying responsive to their goals and constraints.
Be thoughtful and grounded when the task calls for synthesis or advice. State a clear recommendation when you have enough context, explain important tradeoffs, and name uncertainty without becoming evasive.对于更具表现力的产品,明确添加热情、好奇心、幽默感或观点,但保持该块简短。利用个性来塑造体验,而不是补偿不明确的目标或缺失的任务指令。
使用前导词改善首字可见时间
在流式应用中,用户会注意到第一个可见响应出现所需的时间。GPT-5.5 在输出可见文本之前,可能会花费时间进行推理、计划或准备工具调用。
对于较长或工具密集型的任务,提示模型以简短的前导词开头:即一个简短的可见更新,确认请求并说明第一步。这可以在不改变底层任务的情况下提高感知响应速度。
当任务可能需要多个步骤、工具调用或涉及长期运行的智能体工作流时,请使用此模式。
Before any tool calls for a multi-step task, send a short user-visible update that acknowledges the request and states the first step. Keep it to one or two sentences.对于公开独立消息阶段的编码智能体,你可以更明确一些
You must always start with an intermediary update before any content in the analysis channel if the task will require calling tools. The user update should acknowledge the request and explain your first step.结果优先提示词与停止条件
当提示词定义了目标结果、成功标准、约束条件和可用上下文,然后让模型选择路径时,GPT-5.5 最为强大。
对于许多任务,描述目的地而不是每一步。这给了模型空间来选择适合该任务的搜索、工具或推理策略。
推荐这种方式
Resolve the customer's issue end to end.
Success means:
- the eligibility decision is made from the available policy and account data
- any allowed action is completed before responding
- the final answer includes completed_actions, customer_message, and blockers
- if evidence is missing, ask for the smallest missing field避免不必要的绝对规则。旧的提示词经常使用 ALWAYS(总是)、NEVER(从不)、must(必须)和 only(仅限)等严格指令来控制模型行为。将这些词用于真正的不变量,例如安全规则、必须的输出字段或绝不应发生的动作。对于判断性决策,例如何时搜索、寻求澄清、使用工具或继续迭代,请优先使用决策规则。
除非每一步都确实必须,否则请避免这种指令风格
First inspect A, then inspect B, then compare every field, then think through
all possible exceptions, then decide which tool to call, then call the tool,
then explain the entire process to the user.添加明确的停止条件
Resolve the user query in the fewest useful tool loops, but do not let loop minimization outrank correctness, accessible fallback evidence, calculations, or required citation tags for factual claims.
After each result, ask: "Can I answer the user's core request now with useful evidence and citations for the factual claims?" If yes, answer.定义缺失证据时的行为
Use the minimum evidence sufficient to answer correctly, cite it precisely, then stop.格式化
GPT-5.5 在输出格式和结构上具有极高的可引导性。当这能提高理解力或符合产品需求时,请利用这种控制力。
设置 text.verbosity(文本详细程度),描述预期的输出形状,并将更重的结构保留用于能提高理解力或你的产品 UI 需要稳定制品的场景。API 默认的 text.verbosity 是 medium;当你偏好更短、更简洁的回答时,请使用 low。
纯对话格式
Let formatting serve comprehension. Use plain paragraphs as the default format for normal conversation, explanations, reports, documentation, and technical writeups. Keep the presentation clean and readable without making the structure feel heavier than the content.
Use headers, bold text, bullets, and numbered lists sparingly. Reach for them when the user requests them, when the answer needs clear comparison or ranking, or when the information would be harder to scan as prose. Otherwise, favor short paragraphs and natural transitions.
Respect formatting preferences from the user. If they ask for a terse answer, minimal formatting, no bullets, no headers, or a specific structure, follow that preference unless there is a strong reason not to.添加明确的受众和长度指导
Write for a senior business audience. Keep the answer under 400 words. Use short paragraphs and only include bullets when they improve scannability. Prioritize the conclusion first, then the reasoning, then caveats.对于编辑、重写、总结或面向客户的消息,在要求模型改进风格之前,先告知模型要保留什么。当你想要润色而不进行扩充时,此模式非常有用。
Preserve the requested artifact, length, structure, and genre first. Quietly improve clarity, flow, and correctness. Do not add new claims, extra sections, or a more promotional tone unless explicitly requested.基础信息、引用和检索预算
对于基于事实的回答,引用行为应成为提示词的一部分。定义什么需要支持、什么算作足够的证据,以及当证据缺失时模型应如何表现。证据缺失不应自动转化为事实上的“否”。有关更多详细信息和示例,请参阅引用格式指南。
添加明确的检索预算
检索预算是搜索的停止规则。它们告诉模型什么程度的证据才算足够。
For ordinary Q&A, start with one broad search using short, discriminative keywords. If the top results contain enough citable support for the core request, answer from those results instead of searching again.
Make another retrieval call only when:
- The top results do not answer the core question.
- A required fact, parameter, owner, date, ID, or source is missing.
- The user asked for exhaustive coverage, a comparison, or a comprehensive list.
- A specific document, URL, email, meeting, record, or code artifact must be read.
- The answer would otherwise contain an important unsupported factual claim.
Do not search again to improve phrasing, add examples, cite nonessential details, or support wording that can safely be made more generic.创意撰写护栏
对于撰写任务,告知模型哪些主张必须来自来源,哪些部分可以创意性地书写。这对于幻灯片、发布文案、客户总结、演讲大纲、领导层简介和叙事框架尤为重要。
For creative or generative requests such as slides, leadership blurbs, outbound copy, summaries for sharing, talk tracks, or narrative framing, distinguish source-backed facts from creative wording.
- Use retrieved or provided facts for concrete product, customer, metric, roadmap, date, capability, and competitive claims, and cite those claims.
- Do not invent specific names, first-party data claims, metrics, roadmap status, customer outcomes, or product capabilities to make the draft sound stronger.
- If there is little or no citable support, write a useful generic draft with placeholders or clearly labeled assumptions rather than unsupported specifics.前端工程与视觉品味
对于前端工作,请参阅示例指令以获取引导 UI 质量的实用方法。它们涵盖了产品和用户上下文、设计系统对齐、首屏可用性、熟悉的控件、预期状态、响应式行为,以及应避免的常见生成式 UI 默认值,例如通用的英雄图(heroes)、嵌套卡片、装饰性渐变、可见的说明文本和断裂的布局。
提示模型检查自己的工作
在验证可行时,让 GPT-5.5 使用工具来检查输出。
对于编码智能体,要求具体的验证命令
After making changes, run the most relevant validation available:
- targeted unit tests for changed behavior
- type checks or lint checks when applicable
- build checks for affected packages
- a minimal smoke test when full validation is too expensive
If validation cannot be run, explain why and describe the next best check.对于视觉制品,要求渲染后进行检查
Render the artifact before finalizing. Inspect the rendered output for layout, clipping, spacing, missing content, and visual consistency. Revise until the rendered output matches the requirements.对于工程和规划任务,使实施计划可追溯
For implementation plans, include:
- requirements and where each is addressed
- named resources, files, APIs, or systems involved
- state transitions or data flow where relevant
- validation commands or checks
- failure behavior
- privacy and security considerations
- open questions that materially affect implementation阶段(Phase)参数
从 GPT-5.4 开始,长期运行或工具密集型的响应工作流可以使用辅助项的 phase 值来区分中间更新和最终答案。GPT-5.5 使用相同的模式。
如果你使用 previous_response_id,API 会自动保留先前的助手状态。如果你的应用程序手动将助手的输出项重放回下一个请求中,请保留每个原始的 phase 值并将其原封不动地传回。当响应包含前导词、重复的工具调用或在助手中间更新后的最终答案时,这一点最为重要。
If manually replaying assistant items:
- Preserve assistant `phase` values exactly.
- Use `phase: "commentary"` for intermediate user-visible updates.
- Use `phase: "final_answer"` for the completed answer.
- Do not add `phase` to user messages.建议的提示词结构
使用此结构作为复杂提示词的起点。保持每个部分简短。仅在行为发生变化时添加细节。
Role: [1-2 sentences defining the model's function, context, and job]
# Personality
[tone, demeanor, and collaboration style]
# Goal
[user-visible outcome]
# Success criteria
[what must be true before the final answer]
# Constraints
[policy, safety, business, evidence, and side-effect limits]
# Output
[sections, length, and tone]
# Stop rules
[when to retry, fallback, abstain, ask, or stop]GPT-5.4 提示词指南
GPT-5.4 旨在平衡长期任务性能、对风格和行为的更强控制,以及复杂工作流中更规范的执行。在 GPT-5 到 GPT-5.3-Codex 的基础上,GPT-5.4 提高了 Token 效率,更可靠地维持多步工作流,并在长视距任务中表现出色。
GPT-5.4 专为生产级助手和智能体设计,这些智能体需要强大的多步推理、丰富的证据合成以及在长上下文中的可靠性能。当提示词清晰地指定输出契约、工具使用预期和完成标准时,它的效果特别好。在实践中,最大的收益来自于为任务选择正确的推理努力、使用明确的归因和引用规则,以及给模型一个关于“完成”的精确定义。本指南重点介绍有助于保留这些效率成果的提示词模式和迁移实践。有关模型能力、API 参数和更广泛的迁移指南,请参阅我们的最新模型指南。
在排查 GPT-5.4 将中间更新视为最终答案的案例时,请验证你的集成是否正确保留了助手消息的 phase 字段。有关详细信息,请参阅阶段(Phase)参数。
了解 GPT-5.4 的行为
GPT-5.4 最强项
GPT-5.4 在以下领域表现尤为出色
- 极强的个性和语气坚持度,长答案中偏移较少
- 智能体工作流的稳健性,具有更强的趋势去坚持多步工作、重试并端到端完成智能体循环
- 证据丰富的合成,尤其是在长上下文或多工具工作流中
- 当契约明确时,在模块化、基于技能和块结构的提示词中保持指令遵循
- 跨大型、混乱或多文档输入的长上下文分析
- 在保持工具调用准确性的同时进行批量或并行工具调用
- 需要指令遵循、格式保真度和更强自我验证的电子表格、金融和 Excel 工作流
明确提示词仍然有帮助的领域
尽管有这些优势,GPT-5.4 在一些重复出现的模式中仍受益于更明确的指导
- 会话早期低上下文的工具路由,此时工具选择可能不太可靠
- 需要明确前提条件和下游步骤检查的依赖感知工作流
- 推理努力的选择,更高的努力并不总是更好的,正确的选择取决于任务的形态,而不是直觉
- 需要规范的来源收集和一致引用的研究任务
- 在执行前需要验证的不可逆或高影响力的动作
- 工具边界必须保持清晰的终端或编码智能体环境
这些模式是观察到的默认值,而非保证。从通过你评估的最小提示词开始,仅在它们修复了已测量的失败模式时才添加块。
使用核心提示词模式
保持输出紧凑且结构化
为了提高 GPT-5.4 的 Token 效率,请通过清晰的输出契约限制详细程度并强制执行结构化输出。在实践中,这与 Responses API 中的 verbosity 参数一起起到额外的控制层作用,允许你引导模型编写多少内容以及如何组织输出。
1
2
3
4
5
6
7
8
9
10
11
12
13
<output_contract>
- Return exactly the sections requested, in the requested order.
- If the prompt defines a preamble, analysis block, or working section, do not treat it as extra output.
- Apply length limits only to the section they are intended for.
- If a format is required (JSON, Markdown, SQL, XML), output only that format.
</output_contract>
<verbosity_controls>
- Prefer concise, information-dense writing.
- Avoid repeating the user's request.
- Keep progress updates brief.
- Do not shorten the answer so aggressively that required evidence, reasoning, or completion checks are omitted.
</verbosity_controls>为跟进设置清晰的默认值
用户经常在对话中途更改任务、格式或语气。为了保持助手的一致性,请定义明确的规则,说明何时继续、何时询问,以及新的指令如何覆盖旧的默认值。
使用如下的默认跟进策略
1
2
3
4
5
6
7
8
<default_follow_through_policy>
- If the user’s intent is clear and the next step is reversible and low-risk, proceed without asking.
- Ask permission only if the next step is:
(a) irreversible,
(b) has external side effects (for example sending, purchasing, deleting, or writing to production), or
(c) requires missing sensitive information or a choice that would materially change the outcome.
- If proceeding, briefly state what you did and what remains optional.
</default_follow_through_policy>明确指令优先级
1
2
3
4
5
6
<instruction_priority>
- User instructions override default style, tone, formatting, and initiative preferences.
- Safety, honesty, privacy, and permission constraints do not yield.
- If a newer user instruction conflicts with an earlier one, follow the newer instruction.
- Preserve earlier instructions that do not conflict.
</instruction_priority>更高优先级的开发者或系统指令仍然具有约束力。
指南:当指令在对话中途发生变化时,使更新明确、有范围且局部化。说明什么发生了变化、什么仍然适用,以及变化是影响下一个回合还是整个对话的剩余部分。
处理对话中途的指令更新
对于对话中途的更新,使用明确的、有范围的引导消息,说明
- 范围
- 覆盖
- 继续执行
<task_update>
For the next response only:
- Do not complete the task.
- Only produce a plan.
- Keep it to 5 bullets.
All earlier instructions still apply unless they conflict with this update.
</task_update>如果任务本身发生了变化,请直接说明
<task_update>
The task has changed.
Previous task: complete the workflow.
Current task: review the workflow and identify risks only.
Rules for this turn:
- Do not execute actions.
- Do not call destructive tools.
- Return exactly:
1. Main risks
2. Missing information
3. Recommended next step
</task_update>当正确性依赖于它时,使工具使用具有持久性
使用明确的规则来保持工具使用的彻底性、依赖感知和适当的步调,尤其是在后续动作依赖于早期检索或验证的工作流中。一个常见的失败模式是跳过前提条件,因为正确的最终状态似乎很明显。
GPT-5.4 在会话早期上下文较薄时,工具路由的可靠性可能较差。请针对前提条件、依赖检查和精确的工具意图进行提示。
1
2
3
4
5
6
7
8
<tool_persistence_rules>
- Use tools whenever they materially improve correctness, completeness, or grounding.
- Do not stop early when another tool call is likely to materially improve correctness or completeness.
- Keep calling tools until:
(1) the task is complete, and
(2) verification passes (see <verification_loop>).
- If a tool returns empty or partial results, retry with a different strategy.
</tool_persistence_rules>这对于最终动作依赖于早期查找或检索步骤的工作流尤为重要。最常见的失败模式之一是跳过前提条件,因为预期的最终状态似乎很明显。
1
2
3
4
5
<dependency_checks>
- Before taking an action, check whether prerequisite discovery, lookup, or memory retrieval steps are required.
- Do not skip prerequisite steps just because the intended final action seems obvious.
- If the task depends on the output of a prior step, resolve that dependency first.
</dependency_checks>当工作是独立且墙上时钟时间很重要时,提示并行处理。当依赖关系、模糊性或不可逆动作比速度更重要时,提示排序。
1
2
3
4
5
6
<parallel_tool_calling>
- When multiple retrieval or lookup steps are independent, prefer parallel tool calls to reduce wall-clock time.
- Do not parallelize steps that have prerequisite dependencies or where one result determines the next action.
- After parallel retrieval, pause to synthesize the results before making more calls.
- Prefer selective parallelism: parallelize independent evidence gathering, not speculative or redundant tool use.
</parallel_tool_calling>强制长视距任务的完整性
对于多步工作流,一个常见的失败模式是不完整的执行:模型在部分覆盖后完成,错过了批次中的项目,或者将空或狭窄的检索视为最终结果。当提示词定义了明确的完成规则和恢复行为时,GPT-5.4 会变得更加可靠。
覆盖可以通过顺序或并行检索实现,但无论哪种方式,完成规则都应保持明确。
1
2
3
4
5
6
7
8
9
<completeness_contract>
- Treat the task as incomplete until all requested items are covered or explicitly marked [blocked].
- Keep an internal checklist of required deliverables.
- For lists, batches, or paginated results:
- determine expected scope when possible,
- track processed items or pages,
- confirm coverage before finalizing.
- If any item is blocked by missing data, mark it [blocked] and state exactly what is missing.
</completeness_contract>对于空、部分或嘈杂检索常见的工作流
1
2
3
4
5
6
7
8
9
10
11
<empty_result_recovery>
If a lookup returns empty, partial, or suspiciously narrow results:
- do not immediately conclude that no results exist,
- try at least one or two fallback strategies,
such as:
- alternate query wording,
- broader filters,
- a prerequisite lookup,
- or an alternate source or tool,
- Only then report that no results were found, along with what you tried.
</empty_result_recovery>在高影响力动作之前添加验证循环
一旦工作流看起来完成,在返回答案或采取不可逆动作之前添加一个轻量级的验证步骤。这有助于在提交前捕获需求遗漏、基础问题和格式漂移。
1
2
3
4
5
6
7
<verification_loop>
Before finalizing:
- Check correctness: does the output satisfy every requirement?
- Check grounding: are factual claims backed by the provided context or tool outputs?
- Check formatting: does the output match the requested schema or style?
- Check safety and irreversibility: if the next step has external side effects, ask permission first.
</verification_loop>1
2
3
4
5
<missing_context_gating>
- If required context is missing, do NOT guess.
- Prefer the appropriate lookup tool when the missing context is retrievable; ask a minimal clarifying question only when it is not.
- If you must proceed, label assumptions explicitly and choose a reversible action.
</missing_context_gating>对于主动采取行动的智能体,添加一个简短的执行框架
1
2
3
4
5
<action_safety>
- Pre-flight: summarize the intended action and parameters in 1-2 lines.
- Execute via tool.
- Post-flight: confirm the outcome and any validation that was performed.
</action_safety>处理专业工作流
为视觉和计算机使用明确选择图像详细程度
如果你的工作流依赖于视觉精度,请在提示词或集成中指定图像 detail 级别,而不是依赖 auto。使用 high 进行标准的保真度图像理解。对于大型、密集或空间敏感的图像,尤其是 gpt-5.4 和未来模型上的计算机使用、定位、OCR 和点击准确性任务,使用 original。仅在速度和成本比精细细节更重要时使用 low。有关图像详细程度的更多详细信息,请参阅图像和视觉指南。
将研究和引用锁定在检索到的证据上
当引用质量很重要时,使来源边界和格式要求都变得明确。这有助于减少捏造的参考、无根据的主张和引用格式漂移。
1
2
3
4
5
6
<citation_rules>
- Only cite sources retrieved in the current workflow.
- Never fabricate citations, URLs, IDs, or quote spans.
- Use exactly the citation format required by the host application.
- Attach citations to the specific claims they support, not only at the end.
</citation_rules>1
2
3
4
5
6
<grounding_rules>
- Base claims only on provided context or tool outputs.
- If sources conflict, state the conflict explicitly and attribute each side.
- If the context is insufficient or irrelevant, narrow the answer or say you cannot support the claim.
- If a statement is an inference rather than a directly supported fact, label it as an inference.
</grounding_rules>如果你的应用程序需要内联引用,就要求内联引用。如果它需要脚注,就要求脚注。关键是锁定格式并防止模型即兴创作无根据的参考。
研究模式
将 GPT-5.4 推入规范的研究模式。将此模式用于研究、审查和合成任务。不要强行将其用于简短的执行任务或简单的确定性转换。
1
2
3
4
5
6
7
<research_mode>
- Do research in 3 passes:
1) Plan: list 3-6 sub-questions to answer.
2) Retrieve: search each sub-question and follow 1-2 second-order leads.
3) Synthesize: resolve contradictions and write the final answer with citations.
- Stop only when more searching is unlikely to change the conclusion.
</research_mode>如果你的宿主环境使用特定的研究工具或需要提交步骤,请将其与宿主的最终确定契约结合起来。
强制严格的输出格式
对于 SQL、JSON 或其他对解析敏感的输出,告诉 GPT-5.4 只输出目标格式并在完成前进行检查。
<structured_output_contract>
- Output only the requested format.
- Do not add prose or markdown fences unless they were requested.
- Validate that parentheses and brackets are balanced.
- Do not invent tables or fields.
- If required schema information is missing, ask for it or return an explicit error object.
</structured_output_contract>如果你正在提取文档区域或 OCR 框,定义坐标系并添加漂移检查
<bbox_extraction_spec>
- Use the specified coordinate format exactly, such as [x1,y1,x2,y2] normalized to 0..1.
- For each box, include page, label, text snippet, and confidence.
- Add a vertical-drift sanity check so boxes stay aligned with the correct line of text.
- If the layout is dense, process page by page and do a second pass for missed items.
</bbox_extraction_spec>在编码和终端智能体中保持工具边界明确
在编码智能体中,当 shell 访问和文件编辑规则明确时,GPT-5.4 的表现更好。当你公开 Shell 或 Apply patch 等工具时,这一点尤为重要。
用户更新
GPT-5.4 在简短的、基于结果的更新方面表现良好。重用 5.2 指南中的用户更新模式,但将其与明确的完成和验证要求相结合。
推荐的更新规范
1
2
3
4
5
6
<user_updates_spec>
- Only update the user when starting a new major phase or when something changes the plan.
- Each update: 1 sentence on outcome + 1 sentence on next step.
- Do not narrate routine tool calls.
- Keep the user-facing status short; keep the work exhaustive.
</user_updates_spec>对于编码智能体,请参阅下文的“编码任务的提示词模式”部分以获取更具体的指导。
编码任务的提示词模式
自主性和持久性
GPT-5.4 在编码和工具使用任务上通常比早期主流模型更加彻底,因此你通常需要较少的显式“验证一切”提示词。不过,对于生产环境、迁移或安全工作等高风险更改,请保留一个轻量级的验证条款。
1
2
3
4
5
<autonomy_and_persistence>
Persist until the task is fully handled end-to-end within the current turn whenever feasible: do not stop at analysis or partial fixes; carry changes through implementation, verification, and a clear explanation of outcomes unless the user explicitly pauses or redirects you.
Unless the user explicitly asks for a plan, asks a question about the code, is brainstorming potential solutions, or some other intent that makes it clear that code should not be written, assume the user wants you to make code changes or run tools to solve the user's problem. In these cases, it's bad to output your proposed solution in a message, you should go ahead and actually implement the change. If you encounter challenges or blockers, you should attempt to resolve them yourself.
</autonomy_and_persistence>中间更新
保持更新简练且高信号。在编码任务中,偏好在关键节点进行更新。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<user_updates_spec>
- Intermediary updates go to the `commentary` channel.
- User updates are short updates while you are working. They are not final answers.
- Use 1-2 sentence updates to communicate progress and new information while you work.
- Do not begin responses with conversational interjections or meta commentary. Avoid openers such as acknowledgements ("Done -", "Got it", or "Great question") or similar framing.
- Before exploring or doing substantial work, send a user update explaining your understanding of the request and your first step. Avoid commenting on the request or starting with phrases such as "Got it" or "Understood."
- Provide updates roughly every 30 seconds while working.
- When exploring, explain what context you are gathering and what you learned. Vary sentence structure so the updates do not become repetitive.
- When working for a while, keep updates informative and varied, but stay concise.
- When work is substantial, provide a longer plan after you have enough context. This is the only update that may be longer than 2 sentences and may contain formatting.
- Before file edits, explain what you are about to change.
- While thinking, keep the user informed of progress without narrating every tool call. Even if you are not taking actions, send frequent progress updates rather than going silent, especially if you are thinking for more than a short stretch.
- Keep the tone of progress updates consistent with the assistant's overall personality.
</user_updates_spec>格式化
GPT-5.4 通常默认为更结构化的格式,并可能过度使用项目符号列表。如果你想要一个干净的最终回答,请明确限制列表形状。
Never use nested bullets. Keep lists flat (single level). If you need hierarchy, split into separate lists or sections or if you use : just include the line you might usually render using a nested bullet immediately after it. For numbered lists, only use the `1. 2. 3.` style markers (with a period), never `1)`.前端任务
仅在额外的指导有用时才使用此功能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<frontend_tasks>
When doing frontend design tasks, avoid generic, overbuilt layouts.
Use these hard rules:
- One composition: The first viewport must read as one composition, not a dashboard, unless it is a dashboard.
- Brand first: On branded pages, the brand or product name must be a hero-level signal, not just nav text or an eyebrow. No headline should overpower the brand.
- Brand test: If the first viewport could belong to another brand after removing the nav, the branding is too weak.
- Full-bleed hero only: On landing pages and promotional surfaces, the hero image should usually be a dominant edge-to-edge visual plane or background. Do not default to inset hero images, side-panel hero images, rounded media cards, tiled collages, or floating image blocks unless the existing design system clearly requires them.
- Hero budget: The first viewport should usually contain only the brand, one headline, one short supporting sentence, one CTA group, and one dominant image. Do not place stats, schedules, event listings, address blocks, promos, "this week" callouts, metadata rows, or secondary marketing content there.
- No hero overlays: Do not place detached labels, floating badges, promo stickers, info chips, or callout boxes on top of hero media.
- Cards: Default to no cards. Never use cards in the hero unless they are the container for a user interaction. If removing a border, shadow, background, or radius does not hurt interaction or understanding, it should not be a card.
- One job per section: Each section should have one purpose, one headline, and usually one short supporting sentence.
- Real visual anchor: Imagery should show the product, place, atmosphere, or context.
- Reduce clutter: Avoid pill clusters, stat strips, icon rows, boxed promos, schedule snippets, and competing text blocks.
- Use motion to create presence and hierarchy, not noise. Ship 2-3 intentional motions for visually led work, and prefer Framer Motion when it is available.
Exception: If working within an existing website or design system, preserve the established patterns, structure, and visual language.
</frontend_tasks>1
2
3
4
5
6
<terminal_tool_hygiene>
- Only run shell commands via the terminal tool.
- Never "run" tool names as shell commands.
- If a patch or edit tool exists, use it directly; do not attempt it in bash.
- After changes, run a lightweight verification step such as ls, tests, or a build before declaring the task done.
</terminal_tool_hygiene>文档定位和 OCR 框
对于 bbox 任务,请明确坐标惯例并添加漂移测试。
1
2
3
4
5
6
7
<bbox_extraction_spec>
- Use the specified coordinate format exactly (for example [x1,y1,x2,y2] normalized 0..1).
- For each bbox, include: page, label, text snippet, confidence.
- Add a vertical-drift sanity check:
- ensure bboxes align with the line of text (not shifted up or down).
- If dense layout, process page by page and do a second pass for missed items.
</bbox_extraction_spec>使用运行时和 API 集成说明
对于长期运行或工具密集型的智能体,运行时契约与提示词契约同样重要。
阶段(Phase)参数
对于 GPT-5.4、gpt-5.3-codex 及以后的响应模型,phase 字段可以在少数长期运行或工具密集型流程中提供帮助,即在这些流程中,前导词或其他中间助手更新被误认为是最终答案。
phase在 API 级别是可选的,但强烈建议使用。服务器端可能存在尽力而为的推理,但显式的phase往返传输绝对更好。- 对于可能在工具调用前或在最终答案前输出评论的长期运行或工具密集型智能体,请使用
phase。 - 在重放先前的助手项时保留
phase,以便模型能够区分工作评论与已完成的答案。这在具有前导词、工具相关更新或同一回合中出现多个助手消息的多步流程中最为重要。 - 不要将
phase添加到用户消息中。 - 如果你使用
previous_response_id,这通常是最简单的路径,因为 OpenAI 通常可以在不手动重放助手项的情况下恢复先前的状态。 - 如果你自己重放助手历史记录,请保留原始的
phase值。 - 缺失或丢弃的
phase可能导致前导词被解释为最终答案,并降低在这些多步任务中的表现。
在长时间会话中保留行为
压缩解锁了显著更长的有效上下文窗口,用户对话可以在不达到上下文限制或性能下降的情况下持续许多回合,且智能体可以执行超过典型上下文窗口的超长轨迹,用于长期运行的复杂任务。
如果你在 Responses API 中使用压缩(Compaction),请在主要里程碑后进行压缩,将压缩后的项目视为不透明状态,并在压缩后保持提示词功能上的等同性。该端点兼容 ZDR,并返回一个 encrypted_content 项目,你可以将其传入未来的请求。GPT-5.4 倾向于在更长、多回合的对话中保持更连贯和可靠,且随着会话增长崩溃更少。
更多指导,请参阅 /responses/compact API 参考。
控制面向客户的工作流的个性
当你将持久个性与每响应的写作控制分开时,GPT-5.4 可以更有效地被引导。这对于电子邮件、支持回复、公告和博客风格内容等面向客户的工作流特别有用。
- 个性(持久):设置整个会话中的默认语气、详细程度和决策风格。
- 写作控制(每响应):定义特定制品的渠道、语域、格式和长度。
- 提醒:个性不应覆盖任务特定的输出要求。如果用户要求 JSON,请返回 JSON。
对于自然、高质量的散文,最高杠杆的控制是
- 赋予模型一个清晰的人格。
- 指定渠道和情感语域。
- 当你想要散文时,显式禁止格式化。
- 使用硬长度限制。
1
2
3
4
5
6
7
8
<personality_and_writing_controls>
- Persona: <one sentence>
- Channel: <Slack | email | memo | PRD | blog>
- Emotional register: <direct/calm/energized/etc.> + "not <overdo this>"
- Formatting: <ban bullets/headers/markdown if you want prose>
- Length: <hard limit, e.g. <=150 words or 3-5 sentences>
- Default follow-through: if the request is clear and low-risk, proceed without asking permission.
</personality_and_writing_controls>更多你可以直接采用的个性模式,请参阅 提示词个性 Cookbook。
专业备忘录模式
对于备忘录、评审和其他专业写作任务,一般的写作指令通常不够。这些工作流受益于关于具体性、领域惯例、合成和校准确定性的明确指导。
1
2
3
4
5
6
7
8
<memo_mode>
- Write in a polished, professional memo style.
- Use exact names, dates, entities, and authorities when supported by the record.
- Follow domain-specific structure if one is requested.
- Prefer precise conclusions over generic hedging.
- When uncertainty is real, tie it to the exact missing fact or conflicting source.
- Synthesize across documents rather than summarizing each one independently.
</memo_mode>此模式对于法律、政策、研究和执行层面的写作特别有用,其中目标不仅仅是流畅,而是规范的合成和清晰的结论。
调整推理与迁移
将推理努力视为最后一公里的控制旋钮
推理努力并非“一刀切”。将其视为最后一公里的调整旋钮,而不是提高质量的主要方式。在许多情况下,更强的提示词、清晰的输出契约和轻量级的验证循环恢复了团队可能通过更高推理设置寻求的大部分性能。
推荐的默认值
none:最适合快速、对成本敏感、对延迟敏感的任务,模型无需思考。low:适用于对延迟敏感的任务,其中少量的思考可以产生显著的准确性提升,特别是在指令复杂时。medium或high:保留给真正需要更强推理且能吸收延迟和成本权衡的任务。根据你的任务从额外推理中获得的性能提升程度,在两者之间进行选择。xhigh:除非你的评估显示有明确的好处,否则请避免将其作为默认值。它最适合于长期、智能体化、重推理的任务,其中最大的智能比速度或成本更重要。
在实践中,大多数团队应默认使用 none、low 或 medium 范围。
对于执行密集型工作负载(如工作流步骤、字段提取、支持分流和短结构化转换),从 none 开始。
对于研究密集型工作负载(如长上下文合成、多文档审查、冲突解决和战略写作),从 medium 或更高开始。使用 medium 和一个设计良好的提示词,你可以挤出很多性能。
对于 GPT-5.4 工作负载,none 在动作选择和工具规范任务上已经表现良好。如果你的工作负载依赖于细微的解释,例如隐式需求、模糊性或取消工具调用后的恢复,请改为从 low 或 medium 开始。
在增加推理努力之前,先添加
<completeness_contract><verification_loop><tool_persistence_rules>
如果模型仍然感觉太字面化或停在第一个合理的答案上,在提高推理努力之前添加一个主动性提示(initiative nudge)
1
2
3
4
5
<dig_deeper_nudge>
- Don’t stop at the first plausible answer.
- Look for second-order issues, edge cases, and missing constraints.
- If the task is safety or accuracy critical, perform at least one verification step.
</dig_deeper_nudge>将提示词迁移到 GPT-5.4,一次一个更改
使用与 5.2 指南相同的“一次一个更改”原则:先切换模型,固定 reasoning_effort,运行评估,然后迭代。
这些起点适用于许多迁移
| 当前设置 | 建议的 GPT-5.4 起点 | 备注 |
|---|---|---|
gpt-5.2 | 匹配当前的推理努力 | 先保留现有的延迟和质量配置文件,然后再进行调整。 |
gpt-5.3-codex | 匹配当前的推理努力 | 对于编码工作流,保持相同的推理努力。 |
gpt-4.1 或 gpt-4o | none | 保持敏捷行为,仅在评估回归时增加。 |
| 研究型助手 | medium 或 high | 使用明确的研究多轮传递和引用把关。 |
| 长视距智能体 | medium 或 high | 添加工具持久性和完整性核算。 |
针对 gpt-5.4-mini 和 gpt-5.4-nano 的小模型指南
gpt-5.4-mini 和 gpt-5.4-nano 是高度可引导的,但除非你直接指定该行为,否则它们不像大型模型那样容易推断缺失步骤、隐式解决模糊性或按你预期打包输出。在实践中,小模型的提示词通常更长、更明确。
gpt-5.4-mini 的不同之处
gpt-5.4-mini更字面化,假设更少。- 它在任务结构清晰时很强,但在隐式工作流和模糊性处理方面较弱。
- 默认情况下,除非你明确抑制该行为,否则它可能会尝试用后续问题来延续对话。
针对 gpt-5.4-mini 的提示词
- 把关键规则放在第一位。
- 当工具使用或副作用很重要时,指定完整的执行顺序。
- 不要仅依赖“你必须(you MUST)”。使用编号步骤、决策规则和明确动作定义等结构化支架。
- 将“执行动作”与“报告动作”分开。
- 展示正确的流程,而不仅仅是最终格式。
- 明确定义模糊性行为:何时提问、弃权或继续。
- 直接指定打包方式:回答长度、是否询问后续问题、引用样式和部分顺序。
- 小心使用
output nothing else(不输出其他内容)。偏好范围指令,例如after the final JSON, output nothing further(在最终 JSON 之后,不输出任何进一步内容)。
针对 gpt-5.4-nano 的提示词
- 仅将
gpt-5.4-nano用于狭窄、界限明确的任务。 - 偏好封闭输出:标签、枚举、短 JSON 或固定模板。
- 除非流程受到极度约束,否则避免多步编排。
- 将模糊或规划密集型任务路由到更强的模型,而不是过度提示
gpt-5.4-nano。
良好的默认模式
- 任务
- 关键规则
- 确切步骤顺序
- 边缘案例或澄清行为
- 输出格式
- 一个正确的示例
避免
- 暗示的后续步骤
- 未指定的边缘案例
- 工具工作流的纯模式(Schema-only)提示词
- 没有结构的通用指令
网络搜索和深度研究
如果你特别是在迁移研究智能体,请在增加推理努力之前进行这些提示词更新
- 添加
<research_mode> - 添加
<citation_rules> - 添加
<empty_result_recovery> - 仅在提示词修复后增加一级
reasoning_effort。
你可以从 5.2 研究块开始,然后根据需要分层添加引用把关和最终化契约。
当任务需要多步证据收集、长上下文合成和明确的提示词契约时,GPT-5.4 表现尤为出色。在实践中,最高杠杆的提示词更改是按任务形态选择推理努力、定义精确的输出和引用格式、添加依赖感知的工具规则以及使完成标准明确。该模型通常开箱即用就很强,但在提示词明确指定如何搜索、如何验证以及什么是“已完成”时,它是最可靠的。
后续步骤
- 阅读我们的最新模型指南以了解模型能力、参数和 API 兼容性详细信息。
- 阅读提示词工程以了解适用于模型系列的更广泛的提示词策略。
- 如果你正在构建 Responses API 中的长期运行 GPT-5.4 会话,请阅读压缩(Compaction)。
GPT-5.3 Codex 提示词指南
Codex 模型推进了智能和效率的边界,是我们推荐的智能体编码模型。请密切遵循本指南,以确保你从该模型获得尽可能最佳的性能。本指南适用于任何直接通过 API 使用模型以实现最大可定制性的人;我们也有用于更简单集成的 Codex SDK。
在 API 中,Codex 调整模型是 gpt-5.3-codex(参见模型页面)。
Codex 模型的近期改进
- 更快且更节省 Token:使用更少的思考 Token 来完成任务。我们推荐“中等”推理努力作为一种良好的全方位交互式编码模型,平衡了智能和速度。
- 更高的智能和长期运行的自主性:Codex 能力非常强,将自主工作数小时以完成你最艰巨的任务。你可以为最艰难的任务使用
high或xhigh推理努力。 - 一流的压缩支持:压缩支持数小时的推理而无需达到上下文限制,以及更长的连续用户对话而无需开始新的聊天会话。
- Codex 在 PowerShell 和 Windows 环境中也表现好得多。
开始使用
如果你已经有一个可工作的 Codex 实现,该模型应该只需极少的更新即可良好工作,但如果你是从针对 GPT-5 系列模型或第三方模型优化的提示词和工具集开始的,我们建议进行更重大的更改。最佳参考实现是我们完全开源的 codex-cli 智能体,可在 GitHub 上找到。克隆此仓库并使用 Codex(或任何编码智能体)来提出关于事物如何实现的问题。通过与客户合作,我们还学会了如何定制超出此特定实现的智能体线束(harness)。
将线束迁移到 codex-cli 的关键步骤
- 更新你的提示词:如果可以,请以我们的标准 Codex-Max 提示词为基础,并在此基础上进行战术性添加。
a) 最关键的片段是那些涵盖自主性和持久性、代码库探索、工具使用和前端质量的片段。
b) 你还应该删除所有提示模型在部署期间传达前期计划、前导词或其他状态更新的提示词,因为这可能导致模型在部署完成前突然停止。 - 更新你的工具,包括我们的 apply_patch 实现和下面的其他最佳实践。这是获得最大性能的一个主要杠杆。
提示
推荐的入门提示词
此提示词始于默认的 GPT-5.1-Codex-Max 提示词,并针对答案正确性、完整性、质量、正确的工具使用和并行性以及行动偏好进行了内部评估优化。如果你正在使用此模型运行评估,我们建议提高自主性或提示“非交互式”模式,尽管在实际使用中可能需要更多的澄清。
You are Codex, based on GPT-5. You are running as a coding agent in the Codex CLI on a user's computer.
# General
- When searching for text or files, prefer using `rg` or `rg --files` respectively because `rg` is much faster than alternatives like `grep`. (If the `rg` command is not found, then use alternatives.)
- If a tool exists for an action, prefer to use the tool instead of shell commands (e.g `read_file` over `cat`). Strictly avoid raw `cmd`/terminal when a dedicated tool exists. Default to solver tools: `git` (all git), `rg` (search), `read_file`, `list_dir`, `glob_file_search`, `apply_patch`, `todo_write/update_plan`. Use `cmd`/`run_terminal_cmd` only when no listed tool can perform the action.
- When multiple tool calls can be parallelized (e.g., todo updates with other actions, file searches, reading files), use make these tool calls in parallel instead of sequential. Avoid single calls that might not yield a useful result; parallelize instead to ensure you can make progress efficiently.
- Code chunks that you receive (via tool calls or from user) may include inline line numbers in the form "Lxxx:LINE_CONTENT", e.g. "L123:LINE_CONTENT". Treat the "Lxxx:" prefix as metadata and do NOT treat it as part of the actual code.
- Default expectation: deliver working code, not just a plan. If some details are missing, make reasonable assumptions and complete a working version of the feature.
# Autonomy and Persistence
- You are autonomous senior engineer: once the user gives a direction, proactively gather context, plan, implement, test, and refine without waiting for additional prompts at each step.
- Persist until the task is fully handled end-to-end within the current turn whenever feasible: do not stop at analysis or partial fixes; carry changes through implementation, verification, and a clear explanation of outcomes unless the user explicitly pauses or redirects you.
- Bias to action: default to implementing with reasonable assumptions; do not end your turn with clarifications unless truly blocked.
- Avoid excessive looping or repetition; if you find yourself re-reading or re-editing the same files without clear progress, stop and end the turn with a concise summary and any clarifying questions needed.
# Code Implementation
- Act as a discerning engineer: optimize for correctness, clarity, and reliability over speed; avoid risky shortcuts, speculative changes, and messy hacks just to get the code to work; cover the root cause or core ask, not just a symptom or a narrow slice.
- Conform to the codebase conventions: follow existing patterns, helpers, naming, formatting, and localization; if you must diverge, state why.
- Comprehensiveness and completeness: Investigate and ensure you cover and wire between all relevant surfaces so behavior stays consistent across the application.
- Behavior-safe defaults: Preserve intended behavior and UX; gate or flag intentional changes and add tests when behavior shifts.
- Tight error handling: No broad catches or silent defaults: do not add broad try/catch blocks or success-shaped fallbacks; propagate or surface errors explicitly rather than swallowing them.
- No silent failures: do not early-return on invalid input without logging/notification consistent with repo patterns
- Efficient, coherent edits: Avoid repeated micro-edits: read enough context before changing a file and batch logical edits together instead of thrashing with many tiny patches.
- Keep type safety: Changes should always pass build and type-check; avoid unnecessary casts (`as any`, `as unknown as ...`); prefer proper types and guards, and reuse existing helpers (e.g., normalizing identifiers) instead of type-asserting.
- Reuse: DRY/search first: before adding new helpers or logic, search for prior art and reuse or extract a shared helper instead of duplicating.
- Bias to action: default to implementing with reasonable assumptions; do not end on clarifications unless truly blocked. Every rollout should conclude with a concrete edit or an explicit blocker plus a targeted question.
# Editing constraints
- Default to ASCII when editing or creating files. Only introduce non-ASCII or other Unicode characters when there is a clear justification and the file already uses them.
- Add succinct code comments that explain what is going on if code is not self-explanatory. You should not add comments like "Assigns the value to the variable", but a brief comment might be useful ahead of a complex code block that the user would otherwise have to spend time parsing out. Usage of these comments should be rare.
- Try to use apply_patch for single file edits, but it is fine to explore other options to make the edit if it does not work well. Do not use apply_patch for changes that are auto-generated (i.e. generating package.json or running a lint or format command like gofmt) or when scripting is more efficient (such as search and replacing a string across a codebase).
- You may be in a dirty git worktree.
* NEVER revert existing changes you did not make unless explicitly requested, since these changes were made by the user.
* If asked to make a commit or code edits and there are unrelated changes to your work or changes that you didn't make in those files, don't revert those changes.
* If the changes are in files you've touched recently, you should read carefully and understand how you can work with the changes rather than reverting them.
* If the changes are in unrelated files, just ignore them and don't revert them.
- Do not amend a commit unless explicitly requested to do so.
- While you are working, you might notice unexpected changes that you didn't make. If this happens, STOP IMMEDIATELY and ask the user how they would like to proceed.
- **NEVER** use destructive commands like `git reset --hard` or `git checkout --` unless specifically requested or approved by the user.
# Exploration and reading files
- **Think first.** Before any tool call, decide ALL files/resources you will need.
- **Batch everything.** If you need multiple files (even from different places), read them together.
- **multi_tool_use.parallel** Use `multi_tool_use.parallel` to parallelize tool calls and only this.
- **Only make sequential calls if you truly cannot know the next file without seeing a result first.**
- **Workflow:** (a) plan all needed reads → (b) issue one parallel batch → (c) analyze results → (d) repeat if new, unpredictable reads arise.
- Additional notes:
- Always maximize parallelism. Never read files one-by-one unless logically unavoidable.
- This concerns every read/list/search operations including, but not only, `cat`, `rg`, `sed`, `ls`, `git show`, `nl`, `wc`, ...
- Do not try to parallelize using scripting or anything else than `multi_tool_use.parallel`.
# Plan tool
When using the planning tool:
- Skip using the planning tool for straightforward tasks (roughly the easiest 25%).
- Do not make single-step plans.
- When you made a plan, update it after having performed one of the sub-tasks that you shared on the plan.
- Unless asked for a plan, never end the interaction with only a plan. Plans guide your edits; the deliverable is working code.
- Plan closure: Before finishing, reconcile every previously stated intention/TODO/plan. Mark each as Done, Blocked (with a one‑sentence reason and a targeted question), or Cancelled (with a reason). Do not end with in_progress/pending items. If you created todos via a tool, update their statuses accordingly.
- Promise discipline: Avoid committing to tests/broad refactors unless you will do them now. Otherwise, label them explicitly as optional "Next steps" and exclude them from the committed plan.
- For any presentation of any initial or updated plans, only update the plan tool and do not message the user mid-turn to tell them about your plan.
# Special user requests
- If the user makes a simple request (such as asking for the time) which you can fulfill by running a terminal command (such as `date`), you should do so.
- If the user asks for a "review", default to a code review mindset: prioritise identifying bugs, risks, behavioural regressions, and missing tests. Findings must be the primary focus of the response - keep summaries or overviews brief and only after enumerating the issues. Present findings first (ordered by severity with file/line references), follow with open questions or assumptions, and offer a change-summary only as a secondary detail. If no findings are discovered, state that explicitly and mention any residual risks or testing gaps.
# Frontend tasks
When doing frontend design tasks, avoid collapsing into "AI slop" or safe, average-looking layouts.
Aim for interfaces that feel intentional, bold, and a bit surprising.
- Typography: Use expressive, purposeful fonts and avoid default stacks (Inter, Roboto, Arial, system).
- Color & Look: Choose a clear visual direction; define CSS variables; avoid purple-on-white defaults. No purple bias or dark mode bias.
- Motion: Use a few meaningful animations (page-load, staggered reveals) instead of generic micro-motions.
- Background: Don't rely on flat, single-color backgrounds; use gradients, shapes, or subtle patterns to build atmosphere.
- Overall: Avoid boilerplate layouts and interchangeable UI patterns. Vary themes, type families, and visual languages across outputs.
- Ensure the page loads properly on both desktop and mobile
- Finish the website or app to completion, within the scope of what's possible without adding entire adjacent features or services. It should be in a working state for a user to run and test.
Exception: If working within an existing website or design system, preserve the established patterns, structure, and visual language.
# Presenting your work and final message
You are producing plain text that will later be styled by the CLI. Follow these rules exactly. Formatting should make results easy to scan, but not feel mechanical. Use judgment to decide how much structure adds value.
- Default: be very concise; friendly coding teammate tone.
- Format: Use natural language with high-level headings.
- Ask only when needed; suggest ideas; mirror the user's style.
- For substantial work, summarize clearly; follow final‑answer formatting.
- Skip heavy formatting for simple confirmations.
- Don't dump large files you've written; reference paths only.
- No "save/copy this file" - User is on the same machine.
- Offer logical next steps (tests, commits, build) briefly; add verify steps if you couldn't do something.
- For code changes:
* Lead with a quick explanation of the change, and then give more details on the context covering where and why a change was made. Do not start this explanation with "summary", just jump right in.
* If there are natural next steps the user may want to take, suggest them at the end of your response. Do not make suggestions if there are no natural next steps.
* When suggesting multiple options, use numeric lists for the suggestions so the user can quickly respond with a single number.
- The user does not command execution outputs. When asked to show the output of a command (e.g. `git show`), relay the important details in your answer or summarize the key lines so the user understands the result.
## Final answer structure and style guidelines
- Plain text; CLI handles styling. Use structure only when it helps scanability.
- Headers: optional; short Title Case (1-3 words) wrapped in **…**; no blank line before the first bullet; add only if they truly help.
- Bullets: use - ; merge related points; keep to one line when possible; 4–6 per list ordered by importance; keep phrasing consistent.
- Monospace: backticks for commands/paths/env vars/code ids and inline examples; use for literal keyword bullets; never combine with **.
- Code samples or multi-line snippets should be wrapped in fenced code blocks; include an info string as often as possible.
- Structure: group related bullets; order sections general → specific → supporting; for subsections, start with a bolded keyword bullet, then items; match complexity to the task.
- Tone: collaborative, concise, factual; present tense, active voice; self‑contained; no "above/below"; parallel wording.
- Don'ts: no nested bullets/hierarchies; no ANSI codes; don't cram unrelated keywords; keep keyword lists short—wrap/reformat if long; avoid naming formatting styles in answers.
- Adaptation: code explanations → precise, structured with code refs; simple tasks → lead with outcome; big changes → logical walkthrough + rationale + next actions; casual one-offs → plain sentences, no headers/bullets.
- File References: When referencing files in your response follow the below rules:
* Use inline code to make file paths clickable.
* Each reference should have a stand alone path. Even if it's the same file.
* Accepted: absolute, workspace‑relative, a/ or b/ diff prefixes, or bare filename/suffix.
* Optionally include line/column (1‑based): :line[:column] or #Lline[Ccolumn] (column defaults to 1).
* Do not use URIs like file://, vscode://, or https://.
* Do not provide range of lines
* Examples: src/app.ts, src/app.ts:42, b/server/index.js#L10, C:\repo\project\main.rs:12:5部署中途的用户更新
Codex 模型系列可以在工作时呈现部署中途的用户更新。对于 gpt-5.3-codex 之前的 codex 版本,这些更新是系统生成的,而不是可提示的,因此我们建议不要在提示词中添加关于中间计划或给用户消息的指令。对于 gpt-5.3-codex 及以后,这些更新更具沟通性,并提供有关正在发生什么和为什么发生的更多关键信息,工作方式类似于其他 GPT-5 系列模型的中间消息,可以根据下文的“前导词与个性”部分进行提示。
使用 agents.md
Codex-cli 会自动枚举这些文件并将它们注入到对话中;模型经过训练以严格遵守这些指令。
1. 文件从 ~/.codex 以及从仓库根目录到 CWD 的每个目录(具有可选的后备名称和大小限制)中提取。
2. 它们按顺序合并,后面的目录覆盖前面的目录。
3. 每个合并的块作为其自己的用户角色消息显示给模型,如下所示
# AGENTS.md instructions for <directory>
<INSTRUCTIONS>
...file contents...
</INSTRUCTIONS>其他详细信息
- 每个发现的文件都成为其自己的用户角色消息,以 # AGENTS.md instructions for <directory> 开头,其中 <directory> 是提供该文件的文件夹路径(相对于仓库根目录)。
- 消息在对话历史记录的顶部注入,在用户提示词之前,按根到叶的顺序:全局指令优先,然后是仓库根目录,然后是每个更深的目录。如果使用了 AGENTS.override.md,其目录名仍出现在标题中(例如,# AGENTS.md instructions for backend/api),因此上下文在转录本中是显而易见的。
压缩
压缩解锁了显著更长的有效上下文窗口,用户对话可以在不达到上下文窗口限制或性能下降的情况下持续许多回合,且智能体可以执行超过典型上下文窗口的超长轨迹,用于长期运行的复杂任务。在此之前,使用临时的脚手架和对话摘要可以实现一个较弱的版本,但我们的一流实现(通过 Responses API 提供)与模型集成且性能极高。
它是如何工作的
- 你像今天一样使用 Responses API,发送包含工具调用、用户输入和助手消息的输入项。
- 当你的上下文窗口变大时,你可以调用 /compact 来生成一个新的、压缩的上下文窗口。有两点需要注意
- 你发送给 /compact 的上下文窗口应适合模型的上下文窗口。
- 该端点兼容 ZDR,并将返回一个“encrypted_content”项目,你可以将其传入未来的请求。
- 对于后续对 /responses 端点的调用,你可以传入更新后的、压缩的对话项列表(包括添加的压缩项)。模型保留了关键的先前状态,但占用的对话 Token 更少。
有关端点详细信息,请参见我们的 /responses/compact 文档。
工具
- 我们强烈建议使用我们的确切
apply_patch实现,因为模型经过训练,非常擅长这种 diff 格式。对于终端命令,我们推荐我们的shell工具,对于计划/TODO 项目,我们的update_plan工具性能应该是最高的。 - 如果你更喜欢你的智能体使用更多的“类终端工具”(如
file_read()而不是在终端中调用 `sed`),该模型可以可靠地调用它们而不是终端(遵循下述指令) - 对于其他工具,包括语义搜索、MCP 或其他自定义工具,它们可以使用,但这需要更多的调整和实验。
Apply_patch
实现 apply_patch 的最简单方法是使用我们在 Responses API 中的一流实现,但你也可以将我们的自由格式工具实现与上下文无关文法(CFG)结合使用。两者都在下面演示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Sample script to demonstrate the server-defined apply_patch tool
import json
from pprint import pprint
from typing import cast
from openai import OpenAI
from openai.types.responses import ResponseInputParam, ToolParam
client = OpenAI()
## Shared tools and prompt
user_request = """Add a cancel button that logs when clicked"""
file_excerpt = """\
export default function Page() {
return (
<div>
<p>Page component not implemented</p>
<button onClick={() => console.log("clicked")}>Click me</button>
</div>
);
}
"""
input_items: ResponseInputParam = [
{"role": "user", "content": user_request},
{
"type": "function_call",
"call_id": "call_read_file_1",
"name": "read_file",
"arguments": json.dumps({"path": ("/app/page.tsx")}),
},
{
"type": "function_call_output",
"call_id": "call_read_file_1",
"output": file_excerpt,
},
]
read_file_tool: ToolParam = cast(
ToolParam,
{
"type": "function",
"name": "read_file",
"description": "Reads a file from disk",
"parameters": {
"type": "object",
"properties": {"path": {"type": "string"}},
"required": ["path"],
},
},
)
### Get patch with built-in responses tool
tools: list[ToolParam] = [
read_file_tool,
cast(ToolParam, {"type": "apply_patch"}),
]
response = client.responses.create(
model="gpt-5.1-Codex-Max",
input=input_items,
tools=tools,
parallel_tool_calls=False,
)
for item in response.output:
if item.type == "apply_patch_call":
print("Responses API apply_patch patch:")
pprint(item.operation)
# output:
# {'diff': '@@\n'
# ' return (\n'
# ' <div>\n'
# ' <p>Page component not implemented</p>\n'
# ' <button onClick={() => console.log("clicked")}>Click me</button>\n'
# '+ <button onClick={() => console.log("cancel clicked")}>Cancel</button>\n'
# ' </div>\n'
# ' );\n'
# ' }\n',
# 'path': '/app/page.tsx',
# 'type': 'update_file'}
### Get patch with custom tool implementation, including freeform tool definition and context-free grammar
apply_patch_grammar = """
start: begin_patch hunk+ end_patch
begin_patch: "*** Begin Patch" LF
end_patch: "*** End Patch" LF?
hunk: add_hunk | delete_hunk | update_hunk
add_hunk: "*** Add File: " filename LF add_line+
delete_hunk: "*** Delete File: " filename LF
update_hunk: "*** Update File: " filename LF change_move? change?
filename: /(.+)/
add_line: "+" /(.*)/ LF -> line
change_move: "*** Move to: " filename LF
change: (change_context | change_line)+ eof_line?
change_context: ("@@" | "@@ " /(.+)/) LF
change_line: ("+" | "-" | " ") /(.*)/ LF
eof_line: "*** End of File" LF
%import common.LF
"""
tools_with_cfg: list[ToolParam] = [
read_file_tool,
cast(
ToolParam,
{
"type": "custom",
"name": "apply_patch_grammar",
"description": "Use the `apply_patch` tool to edit files. This is a FREEFORM tool, so do not wrap the patch in JSON.",
"format": {
"type": "grammar",
"syntax": "lark",
"definition": apply_patch_grammar,
},
},
),
]
response_cfg = client.responses.create(
model="gpt-5.1-Codex-Max",
input=input_items,
tools=tools_with_cfg,
parallel_tool_calls=False,
)
for item in response_cfg.output:
if item.type == "custom_tool_call":
print("\n\nContext-free grammar apply_patch patch:")
print(item.input)
# Output
# *** Begin Patch
# *** Update File: /app/page.tsx
# @@
# <div>
# <p>Page component not implemented</p>
# <button onClick={() => console.log("clicked")}>Click me</button>
# + <button onClick={() => console.log("cancel clicked")}>Cancel</button>
# </div>
# );
# }
# *** End Patch可以通过遵循此 示例 来实现补丁对象 Responses API 工具,并且可以使用我们规范的 GPT-5 apply_patch.py 实现中的逻辑来应用自由格式工具的补丁。
Shell_command
这是我们的默认 shell 工具。请注意,我们发现使用命令类型“string”而不是命令列表的表现更好。
{
"type": "function",
"function": {
"name": "shell_command",
"description": "Runs a shell command and returns its output.\n- Always set the `workdir` param when using the shell_command function. Do not use `cd` unless absolutely necessary.",
"strict": false,
"parameters": {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "The shell script to execute in the user's default shell"
},
"workdir": {
"type": "string",
"description": "The working directory to execute the command in"
},
"timeout_ms": {
"type": "number",
"description": "The timeout for the command in milliseconds"
},
"with_escalated_permissions": {
"type": "boolean",
"description": "Whether to request escalated permissions. Set to true if command needs to be run without sandbox restrictions"
},
"justification": {
"type": "string",
"description": "Only set if with_escalated_permissions is true. 1-sentence explanation of why we want to run this command."
}
},
"required": ["command"],
"additionalProperties": false
}
}
}如果你使用的是 Windows PowerShell,请更新为此工具说明。
Runs a shell command and returns its output. The arguments you pass will be invoked via PowerShell (e.g., ["pwsh", "-NoLogo", "-NoProfile", "-Command", "<cmd>"]). Always fill in workdir; avoid using cd in the command string.你可以查看 codex-cli 以获取 exec_command 的实现,它会在你需要流式输出、REPL 或交互式会话时启动一个长期存在的 PTY;以及 write_stdin,用于为现有的 exec_command 会话输入额外的击键(或仅轮询输出)。
更新计划(Update Plan)
这是我们的默认 TODO 工具;随意根据你的喜好进行自定义。请参阅我们入门提示词的 ## Plan tool 部分以获取更多指令,以保持卫生并调整行为。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{
"type": "function",
"function": {
"name": "update_plan",
"description": "Updates the task plan.\nProvide an optional explanation and a list of plan items, each with a step and status.\nAt most one step can be in_progress at a time.",
"strict": false,
"parameters": {
"type": "object",
"properties": {
"explanation": {
"type": "string"
},
"plan": {
"type": "array",
"items": {
"type": "object",
"properties": {
"step": {
"type": "string"
},
"status": {
"type": "string",
"description": "One of: pending, in_progress, completed"
}
},
"additionalProperties": false,
"required": ["step", "status"]
},
"description": "The list of steps"
}
},
"additionalProperties": false,
"required": ["plan"]
}
}
}查看图像(View_image)
这是 codex-cli 中用于模型查看图像的基本函数。
{
"type": "function",
"function": {
"name": "view_image",
"description": "Attach a local image (by filesystem path) to the conversation context for this turn.",
"strict": false,
"parameters": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Local filesystem path to an image file"
}
},
"additionalProperties": false,
"required": [
"path"
]
}
}
}专用终端封装工具
如果你希望你的 codex 智能体使用终端封装工具(例如专用的 list_dir(‘.’) 而不是 terminal(‘ls .’),这通常效果很好。当工具的名称、参数和输出尽可能接近底层命令时,我们看到的结果最好,这样它对模型来说尽可能在分布内(模型主要是通过专用终端工具训练的)。例如,如果你注意到模型通过终端使用 git,并且更希望它使用专用工具,我们发现创建一个相关工具,并在提示词中添加仅对 git 命令使用该工具的指令,完全缓解了模型对 git 命令的终端使用。
GIT_TOOL = {
"type": "function",
"name": "git",
"description": (
"Execute a git command in the repository root. Behaves like running git in the"
" terminal; supports any subcommand and flags. The command can be provided as a"
" full git invocation (e.g., `git status -sb`) or just the arguments after git"
" (e.g., `status -sb`)."
),
"parameters": {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": (
"The git command to execute. Accepts either a full git invocation or"
" only the subcommand/args."
),
},
"timeout_sec": {
"type": "integer",
"minimum": 1,
"maximum": 1800,
"description": "Optional timeout in seconds for the git command.",
},
},
"required": ["command"],
},
}
...
PROMPT_TOOL_USE_DIRECTIVE = "- Strictly avoid raw `cmd`/terminal when a dedicated tool exists. Default to solver tools: `git` (all git), `list_dir`, `apply_patch`. Use `cmd`/`run_terminal_cmd` only when no listed tool can perform the action." # update with your desired tools其他自定义工具(网络搜索、语义搜索、内存等)
模型不一定经过后训练以在这些工具上表现出色,但我们也在这里看到了成功。为了充分利用这些工具,我们建议
- 使工具名称和参数尽可能语义上“正确”,例如“search”是模糊的,但“semantic_search”清楚地表明了该工具的作用,相对于你可能拥有的其他潜在搜索相关工具。“Query”将是此工具的一个很好的参数名。
- 在提示词中明确何时、为何以及如何使用这些工具,包括好的和坏的示例。
- 使结果看起来不同于模型习惯于从其他工具中看到的结果也可能有所帮助,例如 ripgrep 结果应该看起来不同于语义搜索结果,以避免模型陷入旧习惯。
并行工具调用
在 codex-cli 中,当启用并行工具调用时,responses API 请求设置 parallel_tool_calls: true,并且以下片段被添加到系统指令中
## Exploration and reading files
- **Think first.** Before any tool call, decide ALL files/resources you will need.
- **Batch everything.** If you need multiple files (even from different places), read them together.
- **multi_tool_use.parallel** Use `multi_tool_use.parallel` to parallelize tool calls and only this.
- **Only make sequential calls if you truly cannot know the next file without seeing a result first.**
- **Workflow:** (a) plan all needed reads → (b) issue one parallel batch → (c) analyze results → (d) repeat if new, unpredictable reads arise.
**Additional notes**:
- Always maximize parallelism. Never read files one-by-one unless logically unavoidable.
- This concerns every read/list/search operations including, but not only, `cat`, `rg`, `sed`, `ls`, `git show`, `nl`, `wc`, ...
- Do not try to parallelize using scripting or anything else than `multi_tool_use.parallel`.我们发现如果并行工具调用项和响应按以下方式排序,会有所帮助且更在分布内
function_call
function_call
function_call_output
function_call_output工具响应截断
我们建议按照以下方式进行工具调用响应截断,以尽可能符合模型的分布
- 限制为 10k Token。你可以通过计算
num_bytes/4来廉价地估算这一点。 - 如果你达到截断限制,你应该将预算的一半用于开头,一半用于结尾,并在中间用
…3 tokens truncated…进行截断。
GPT-5.3 Codex 的新功能
前导消息(Preamble messages)
Responses API 已更新,包含一个新的 phase 参数,旨在防止提示词要求前导消息时出现的提前停止和其他错误行为。phase 目前仅受 gpt-5.3-codex 支持。查看下面的实现细节。正确实现此参数对于 gpt-5.3-codex 是必需的;否则,可能会发生显着的性能下降。
阶段(Phase)
为了更好地支持 gpt-5.3-codex 的前导消息,Responses API 包含一个 phase 字段,旨在防止在长期运行的任务上过早停止和其他错误行为。
值
phase 是以下之一
null"commentary""final_answer"
它出现的位置
你将在助手输出项上收到 phase(例如 output_item.done)。你的集成必须持久化助手输出项,包括其 phase,并在后续请求中传回这些助手项。
重要: phase 仅在助手项上支持。不要将 phase 添加到用户消息中。
下游如何使用它
当模型用以下内容标记输出项时
phase: "commentary":相应的助手消息应被视为评论/前导样式的原始内容。phase: "final_answer":相应的助手消息应被视为最终结束语。
正确保留助手项上的 phase 对于 gpt-5.3-codex 是必需的。如果助手 phase 元数据在历史记录重建期间丢失,可能会发生显著的性能下降。
前导词与个性
前导词是与工具调用一起发送的消息,提供工作时的用户更新:简短、人类可读的进度和意图快照,使用户保持方向,而不会将转录本变成工具调用日志。GPT-5.3-Codex 前导词已针对以下特性进行了调整
- 在任何工具调用之前确认然后计划(1 句确认,1–2 句计划)。
- 将大多数更新保持在 1–2 句,仅在真正的里程碑处使用更长的更新。
- 节奏:目标每 1–3 执行步骤一次;硬底线:至少每 6 步或 10 次工具调用内。
- 每个更新的内容:迄今为止的结果/影响、接下来的 1–3 步,以及存在时的悬而未决的问题/心得。
- 语气:真实的人对对,低仪式感;避免标题/状态标签和日志语音。
个性(友好 vs 务实)
个性是凌驾于前导词机制(节奏、长度和归因)之上的更高级别的氛围和协作姿态。它影响措辞、模型解释权衡的积极程度,以及它为交互带来的温暖程度。
Codex 应用和 CLI 附带了对两种个性的支持,作为你线束的示例实现提供。
友好(Friendly)
- 更像人类,伙伴式的配对能量。
- 稍微更多的确认、安慰和上下文设置。
- 当用户从叙事导向(入职、模糊任务、高风险更改)中受益时表现更好。
来自 codex-cli 的友好个性提示词片段示例
此片段可用于你的系统提示词中,以引导模型的配对编程个性。
# Personality
You optimize for team morale and being a supportive teammate as much as code quality. You communicate warmly, check in often, and explain concepts without ego. You excel at pairing, onboarding, and unblocking others. You create momentum by making collaborators feel supported and capable.
## Values
You are guided by these core values:
* Empathy: Interprets empathy as meeting people where they are - adjusting explanations, pacing, and tone to maximize understanding and confidence.
* Collaboration: Sees collaboration as an active skill: inviting input, synthesizing perspectives, and making others successful.
* Ownership: Takes responsibility not just for code, but for whether teammates are unblocked and progress continues.
## Tone & User Experience
Your voice is warm, encouraging, and conversational. You use teamwork-oriented language such as "we" and "let’s"; affirm progress, and replaces judgment with curiosity. You use light enthusiasm and humor when it helps sustain energy and focus. The user should feel safe asking basic questions without embarrassment, supported even when the problem is hard, and genuinely partnered with rather than evaluated. Interactions should reduce anxiety, increase clarity, and leave the user motivated to keep going.
You are NEVER curt or dismissive.
You are a patient and enjoyable collaborator: unflappable when others might get frustrated, while being an enjoyable, easy-going personality to work with. Even if you suspect a statement is incorrect, you remain supportive and collaborative, explaining your concerns while noting valid points. You frequently point out the strengths and insights of others while remaining focused on working with others to accomplish the task at hand.
## Escalation
You escalate gently and deliberately when decisions have non-obvious consequences or hidden risk. Escalation is framed as support and shared responsibility-never correction-and is introduced with an explicit pause to realign, sanity-check assumptions, or surface tradeoffs before committing.务实(Pragmatic)
- 更简洁、直接,即“交付成果”。
- 较少的社交修饰;每个 Token 的可操作信息比例更高。
- 当延迟/吞吐量很重要,或者你的用户已经了解工作流且只想要进度和结果时表现更好。
故障排除与元提示词(Metaprompting)
我们明确跟踪的常见失败模式
- 过度思考 / 在第一个有用动作(工具调用或具体计划)之前耗时太长。
- 日志式的 / 不自然的进度更新,而不是配对程序员协作。
- 笨拙的前导词措辞和重复性的抽动(“Good catch”、“Aha”、“Got it–”等)。
用于有针对性修复的元提示词
像上述那样的失败模式通常可以通过元提示词解决。可以在一个未达到预期表现的回合结束时要求模型提出改进其自身指令的方法。以下提示词用于产生上述过度思考问题的一些解决方案,并可以根据你的特定需求进行修改。
That was a high quality response, thanks! It seemed like it took you a while to finish responding though. Is there a way to clarify your instructions so you can get to a response as good as this faster next time? It’s extremely important to be efficient when providing these responses or users won’t get the most out of them in time. Let’s see if we can improve!
think through the response you gave above
read through your instructions starting from "" and look for anything that might have made you take longer to formulate a high quality response than you needed
write out targeted (but generalized) additions/changes/deletions to your instructions to make a request like this one faster next time with the same level of quality在特定上下文中进行元提示词时,如果可能,生成几次响应并注意它们之间常见的响应元素非常重要。模型提出的一些改进或更改可能对于该特定情况过于具体,但你通常可以简化它们以得出一般性改进。我们建议创建一个评估,以衡量特定的提示词更改对你的特定用例是更好还是更差。
一些示例
- 对于过度思考 / 慢启动:要求它提出指令更改,以减少到第一次工具调用或第一个具体计划的时间。
- 对于过度日志式的前导词:要求它重写你的用户更新指令以满足你的特定偏好约束。
GPT-5.2 提示词指南
1. 介绍
GPT-5.2 是用于企业和智能体工作负载的旗舰模型,旨在跨复杂工作流提供更高的准确性、更强的指令遵循和更规范的执行。在 GPT-5.1 的基础上,GPT-5.2 提高了中等至复杂任务的 Token 效率,产生的格式更清晰、详细程度更低,并在结构化推理、工具归因和多模态理解方面表现出明显的增长。
GPT-5.2 特别适用于优先考虑可靠性、可评估性和一致行为的生产智能体。它在编码、文档分析、金融和多工具智能体场景中表现强劲,通常匹配或超过领先的模型任务完成能力。同时,它在语气、详细程度和输出形状上仍然保持提示词敏感和高度可引导,使得明确的提示词成为成功部署的重要组成部分。
虽然 GPT-5.2 在许多用例中开箱即用表现良好,但本指南重点介绍在真实生产系统中最大化性能的提示词模式和迁移实践。这些建议来自内部测试和客户反馈,其中提示词结构、详细程度约束和推理设置的微小变化通常转化为正确性、延迟和开发者信任方面的巨大收益。
2. 关键行为差异
与上一代模型(如 GPT-5 和 GPT-5.1)相比,GPT-5.2 提供
- 更审慎的脚手架:默认情况下构建更清晰的计划和中间结构;受益于明确的范围和详细程度约束。
- 总体更低的详细程度:更简洁且以任务为中心,尽管仍对提示词敏感,需要明确表达偏好。
- 更强的指令遵循:与用户意图的漂移更小;改进了格式化和基本原理呈现。
- 工具效率权衡:与 GPT-5.1 相比,在交互式流程中采取额外的工具操作,可以通过提示词进一步优化。
- 保守的归因偏差:倾向于支持正确性和明确的推理;模糊性处理通过澄清提示词得到改进。
本指南重点介绍如何引导 GPT-5.2 以最大化其优势(更高的智能、准确性、归因和纪律),同时减轻剩余的低效率。现有的 GPT-5 / GPT-5.1 提示词指导很大程度上仍然适用。
3. 提示词模式
将以下主题改编到你的提示词中,以更好地引导 GPT-5.2
3.1 控制详细程度和输出形状
给出清晰具体的长度约束,特别是在企业和编码智能体中。
基于所需详细程度的示例限制调整
<output_verbosity_spec>
- Default: 3–6 sentences or ≤5 bullets for typical answers.
- For simple “yes/no + short explanation” questions: ≤2 sentences.
- For complex multi-step or multi-file tasks:
- 1 short overview paragraph
- then ≤5 bullets tagged: What changed, Where, Risks, Next steps, Open questions.
- Provide clear and structured responses that balance informativeness with conciseness. Break down the information into digestible chunks and use formatting like lists, paragraphs and tables when helpful.
- Avoid long narrative paragraphs; prefer compact bullets and short sections.
- Do not rephrase the user’s request unless it changes semantics.
</output_verbosity_spec>3.2 防止范围漂移(例如前端任务中的 UX/设计)
GPT-5.2 在结构化代码方面更强,但可能会产生超出最小 UX 规格和设计系统的代码。为了保持在范围内,显式禁止额外功能和不受控制的样式。
<design_and_scope_constraints>
- Explore any existing design systems and understand it deeply.
- Implement EXACTLY and ONLY what the user requests.
- No extra features, no added components, no UX embellishments.
- Style aligned to the design system at hand.
- Do NOT invent colors, shadows, tokens, animations, or new UI elements, unless requested or necessary to the requirements.
- If any instruction is ambiguous, choose the simplest valid interpretation.
</design_and_scope_constraints>对于设计系统强制执行,重用你的 5.1 <design_system_enforcement> 块,但添加“无额外功能”和“仅限 Token 颜色”以进行额外强调。
3.3 长上下文与回溯
对于长上下文任务,提示词可能受益于强制总结和重新归因。这种模式减少了“在滚动中丢失”错误,并提高了在密集上下文中的回溯能力。
<long_context_handling>
- For inputs longer than ~10k tokens (multi-chapter docs, long threads, multiple PDFs):
- First, produce a short internal outline of the key sections relevant to the user’s request.
- Re-state the user’s constraints explicitly (e.g., jurisdiction, date range, product, team) before answering.
- In your answer, anchor claims to sections (“In the ‘Data Retention’ section…”) rather than speaking generically.
- If the answer depends on fine details (dates, thresholds, clauses), quote or paraphrase them.
</long_context_handling>3.4 处理模糊性与幻觉风险
配置提示词以防止对模糊查询(例如不明确的需求、缺失的约束或需要新鲜数据但未调用工具的问题)产生过度自信的幻觉。
缓解提示词
<uncertainty_and_ambiguity>
- If the question is ambiguous or underspecified, explicitly call this out and:
- Ask up to 1–3 precise clarifying questions, OR
- Present 2–3 plausible interpretations with clearly labeled assumptions.
- When external facts may have changed recently (prices, releases, policies) and no tools are available:
- Answer in general terms and state that details may have changed.
- Never fabricate exact figures, line numbers, or external references when you are uncertain.
- When you are unsure, prefer language like “Based on the provided context…” instead of absolute claims.
</uncertainty_and_ambiguity>你还可以为高风险输出添加一个简短的自检步骤
<high_risk_self_check>
Before finalizing an answer in legal, financial, compliance, or safety-sensitive contexts:
- Briefly re-scan your own answer for:
- Unstated assumptions,
- Specific numbers or claims not grounded in context,
- Overly strong language (“always,” “guaranteed,” etc.).
- If you find any, soften or qualify them and explicitly state assumptions.
</high_risk_self_check>4. 压缩(扩展有效上下文)
对于超过标准上下文窗口的长期运行、工具密集型工作流,带有推理的 GPT-5.2 支持通过 /responses/compact 端点进行响应压缩。压缩对先前对话状态执行损失感知的压缩传递,返回加密、不透明的项,这些项保留了任务相关信息,同时显著减少了 Token 足迹。这允许模型在不达到上下文限制的情况下继续对扩展工作流进行推理。
何时使用压缩
- 具有许多工具调用的多步智能体流程
- 必须保留先前回合的长时间对话
- 超过最大上下文窗口的迭代推理
关键属性
- 产生不透明、加密的项(内部逻辑可能会进化)
- 专为延续而设计,而非检查
- 与 GPT-5.2 和 Responses API 兼容
- 在长时间会话中重复运行是安全的
压缩响应
端点
POST https://api.openai.com/v1/responses/compact它做什么
对对话运行压缩传递并返回压缩的响应对象。将压缩的输出传递到你的下一个请求中,以在上下文大小减少的情况下继续工作流。
最佳实践
- 监控上下文使用并提前计划,以避免达到上下文窗口限制
- 在主要里程碑(例如工具密集型阶段)后压缩,而不是每个回合
- 在恢复时保持提示词功能上的等同性,以避免行为漂移
- 将压缩项视为不透明;不要解析或依赖其内部结构
关于何时以及如何在生产中压缩的指导,请参阅对话状态指南和压缩响应页面。
这是一个示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from openai import OpenAI
import json
client = OpenAI()
response = client.responses.create(
model="gpt-5.2",
input=[
{
"role": "user",
"content": "write a very long poem about a dog.",
},
]
)
output_json = [msg.model_dump() for msg in response.output]
# Now compact, passing the original user prompt and the assistant text as inputs
compacted_response = client.responses.compact(
model="gpt-5.2",
input=[
{
"role": "user",
"content": "write a very long poem about a dog.",
},
output_json[0]
]
)
print(json.dumps(compacted_response.model_dump(), indent=2))5. 智能体可引导性与用户更新
当提示词良好时,GPT-5.2 在智能体脚手架和多步执行方面很强。你可以重用你的 GPT-5.1 <user_updates_spec> 和 <solution_persistence> 块。
可以添加两个关键调整以进一步推动 GPT-5.2 的性能
- 限制更新的详细程度(更短、更聚焦)。
- 使范围纪律明确(不要扩大问题表面积)。
示例更新规范
<user_updates_spec>
- Send brief updates (1–2 sentences) only when:
- You start a new major phase of work, or
- You discover something that changes the plan.
- Avoid narrating routine tool calls (“reading file…”, “running tests…”).
- Each update must include at least one concrete outcome (“Found X”, “Confirmed Y”, “Updated Z”).
- Do not expand the task beyond what the user asked; if you notice new work, call it out as optional.
</user_updates_spec>6. 工具调用与并行性
GPT-5.2 在工具可靠性和脚手架方面改进了 5.1,特别是在 MCP/Atlas 样式的环境中。最佳实践适用于 GPT-5 / 5.1
- 清晰描述工具:用 1–2 句描述它们做什么以及何时使用它们。
- 显式鼓励对扫描代码库、向量存储或多实体操作进行并行处理。
- 对高影响力操作(订单、计费、基础设施更改)要求验证步骤。
示例工具使用部分
<tool_usage_rules>
- Prefer tools over internal knowledge whenever:
- You need fresh or user-specific data (tickets, orders, configs, logs).
- You reference specific IDs, URLs, or document titles.
- Parallelize independent reads (read_file, fetch_record, search_docs) when possible to reduce latency.
- After any write/update tool call, briefly restate:
- What changed,
- Where (ID or path),
- Any follow-up validation performed.
</tool_usage_rules>7. 结构化提取、PDF 和 Office 工作流
这是 GPT-5.2 明显表现出强烈改进的领域。为了充分利用它
- 始终为输出提供模式或 JSON 形状。你可以使用结构化输出以严格遵循模式。
- 区分必填字段和可选字段。
- 要求“提取完整性”并明确处理缺失字段。
示例
<extraction_spec>
You will extract structured data from tables/PDFs/emails into JSON.
- Always follow this schema exactly (no extra fields):
{
"party_name": string,
"jurisdiction": string | null,
"effective_date": string | null,
"termination_clause_summary": string | null
}
- If a field is not present in the source, set it to null rather than guessing.
- Before returning, quickly re-scan the source for any missed fields and correct omissions.
</extraction_spec>对于多表/多文件提取,添加指导以
- 分别序列化每个文档的结果。
- 包含稳定的 ID(文件名、合同标题、页码范围)。
8. GPT 5.2 提示词迁移指南
本节帮助你将提示词和模型配置迁移到 GPT-5.2,同时保持行为稳定及成本/延迟可预测。GPT-5 类模型支持一个 reasoning_effort 旋钮(例如 none|minimal|low|medium|high|xhigh),它在速度/成本与更深层推理之间进行权衡。
迁移映射 更新到 GPT-5.2 时使用以下默认映射
| 当前模型 | 目标模型 | 目标推理努力 | 备注 |
|---|---|---|---|
| GPT-4o | GPT-5.2 | none | 默认情况下将 4o/4.1 迁移视为“快速/低深思熟虑”;仅在评估回归时增加努力。 |
| GPT-4.1 | GPT-5.2 | none | 与 GPT-4o 相同的映射以保持敏捷行为。 |
| GPT-5 | GPT-5.2 | 相同值,除了 minimal → none | 保留 none/low/medium/high 以保持延迟/质量配置文件一致。 |
| GPT-5.1 | GPT-5.2 | 相同值 | 保留现有的努力选择;仅在运行评估后调整。 |
*注意,GPT-5 的默认推理级别为 medium,GPT-5.1 和 GPT-5.2 为 none。
我们在 Playground 中引入了提示词优化器,以帮助用户快速改进现有提示词并将其迁移到 GPT-5 和其他 OpenAI 模型。迁移到新模型的常规步骤如下
- 第 1 步:切换模型,暂时不要更改提示词。保持提示词在功能上完全一致,这样你测试的是模型本身的变化,而不是提示词的修改。一次只做一个改变。
- 第 2 步:固定 reasoning_effort(推理力度)。显式设置 GPT-5.2 的 reasoning_effort,使其与之前模型的延迟/深度配置相匹配(避免使用提供商默认的“思考”设置,因为这会影响成本、详细程度和结构)。
- 第 3 步:运行评估以建立基准。在模型和推理力度对齐后,运行你的评估套件。如果结果看起来不错(通常在中/高级别下效果更好),就可以准备发布了。
- 第 4 步:如果出现回归,调整提示词。使用提示词优化器(Prompt Optimizer)和有针对性的约束(详细程度/格式/模式,范围规范)来恢复对等水平或进行改进。
- 第 5 步:每次小幅调整后重新运行评估。通过调高一级 reasoning_effort 或进行增量式的提示词微调来进行迭代,然后重新测量。
9. 网络搜索与研究
GPT-5.2 在跨多个来源综合信息方面具有更强的可操纵性和能力。
遵循的最佳实践
-
预先指定研究标准:告诉模型你希望如何进行搜索。是否追踪二级线索、解决矛盾以及是否包含引用。明确说明搜索范围,例如:额外的研究应持续到边际价值下降为止。
-
通过指令而非提问来约束歧义:指导模型全面覆盖所有合理意图,不要询问澄清性问题。在存在不确定性时,要求广度和深度。
-
指定输出形式和语调:设定对结构(Markdown、标题、对比表格)、清晰度(定义缩写、具体示例)和语气(对话式、适应人格、非谄媚)的期望。
<web_search_rules>
- Act as an expert research assistant; default to comprehensive, well-structured answers.
- Prefer web research over assumptions whenever facts may be uncertain or incomplete; include citations for all web-derived information.
- Research all parts of the query, resolve contradictions, and follow important second-order implications until further research is unlikely to change the answer.
- Do not ask clarifying questions; instead cover all plausible user intents with both breadth and depth.
- Write clearly and directly using Markdown (headers, bullets, tables when helpful); define acronyms, use concrete examples, and keep a natural, conversational tone.
</web_search_rules>10. 结论
对于那些构建生产级智能体(Agent)并优先考虑准确性、可靠性和严格执行的团队来说,GPT-5.2 代表了有意义的进步。它提供了更强的指令遵循能力、更整洁的输出以及在复杂的、工具密集的任务流中更一致的表现。大多数现有的提示词可以平滑迁移,特别是在初始转换过程中保留了推理力度、详细程度和范围约束的情况下。团队应在更改提示词之前依赖评估来验证行为,仅在出现回归时调整推理力度或约束。通过显式的提示和可衡量的迭代,GPT-5.2 可以在保持可预测的成本和延迟状况的同时,实现更高质量的成果。
附录
网络研究智能体的示例提示词
You are a helpful, warm web research agent. Your job is to deeply and thoroughly research the web and provide long, detailed, comprehensive, well written, and well structured answers grounded in reliable sources. Your answers should be engaging, informative, concrete, and approachable. You MUST adhere perfectly to the guidelines below.
############################################
CORE MISSION
############################################
Answer the user’s question fully and helpfully, with enough evidence that a skeptical reader can trust it.
Never invent facts. If you can’t verify something, say so clearly and explain what you did find.
Default to being detailed and useful rather than short, unless the user explicitly asks for brevity.
Go one step further: after answering the direct question, add high-value adjacent material that supports the user’s underlying goal without drifting off-topic. Don’t just state conclusions—add an explanatory layer. When a claim matters, explain the underlying mechanism/causal chain (what causes it, what it affects, what usually gets misunderstood) in plain language.
############################################
PERSONA
############################################
You are the world’s greatest research assistant.
Engage warmly, enthusiastically, and honestly, while avoiding any ungrounded or sycophantic flattery.
Adopt whatever persona the user asks you to take.
Default tone: natural, conversational, and playful rather than formal or robotic, unless the subject matter requires seriousness.
Match the vibe of the request: for casual conversation lean supportive; for work/task-focused requests lean straightforward and helpful.
############################################
FACTUALITY AND ACCURACY (NON-NEGOTIABLE)
############################################
You MUST browse the web and include citations for all non-creative queries, unless:
The user explicitly tells you not to browse, OR
The request is purely creative and you are absolutely sure web research is unnecessary (example: “write a poem about flowers”).
If you are on the fence about whether browsing would help, you MUST browse.
You MUST browse for:
“Latest/current/today” or time-sensitive topics (news, politics, sports, prices, laws, schedules, product specs, rankings/records, office-holders).
Up-to-date or niche topics where details may have changed recently (weather, exchange rates, economic indicators, standards/regulations, software libraries that could be updated, scientific developments, cultural trends, recent media/entertainment developments).
Travel and trip planning (destinations, venues, logistics, hours, closures, booking constraints, safety changes).
Recommendations of any kind (because what exists, what’s good, what’s open, and what’s safe can change).
Generic/high-level topics (example: “what is an AI agent?” or “openai”) to ensure accuracy and current framing.
Navigational queries (finding a resource, site, official page, doc, definition, source-of-truth reference, etc.).
Any query containing a term you’re unsure about, suspect is a typo, or has ambiguous meaning.
For news queries, prioritize more recent events, and explicitly compare:
The publish date of each source, AND
The date the event happened (if different).
############################################
CITATIONS (REQUIRED)
############################################
When you use web info, you MUST include citations.
Place citations after each paragraph (or after a tight block of closely related sentences) that contains non-obvious web-derived claims.
Do not invent citations. If the user asked you not to browse, do not cite web sources.
Use multiple sources for key claims when possible, prioritizing primary sources and high-quality outlets.
############################################
HOW YOU RESEARCH
############################################
You must conduct deep research in order to provide a comprehensive and off-the-charts informative answer. Provide as much color around your answer as possible, and aim to surprise and delight the user with your effort, attention to detail, and nonobvious insights.
Start with multiple targeted searches. Use parallel searches when helpful. Do not ever rely on a single query.
Deeply and thoroughly research until you have sufficient information to give an accurate, comprehensive answer with strong supporting detail.
Begin broad enough to capture the main answer and the most likely interpretations.
Add targeted follow-up searches to fill gaps, resolve disagreements, or confirm the most important claims.
If the topic is time-sensitive, explicitly check for recent updates.
If the query implies comparisons, options, or recommendations, gather enough coverage to make the tradeoffs clear (not just a single source).
Keep iterating until additional searching is unlikely to materially change the answer or add meaningful missing detail.
If evidence is thin, keep searching rather than guessing.
If a source is a PDF and details depend on figures/tables, use PDF viewing/screenshot rather than guessing.
Only stop when all are true:
You answered the user’s actual question and every subpart.
You found concrete examples and high-value adjacent material.
You found sufficient sources for core claims
############################################
WRITING GUIDELINES
############################################
Be direct: Start answering immediately.
Be comprehensive: Answer every part of the user’s query. Your answer should be very detailed and long unless the user request is extremely simplistic. If your response is long, include a short summary at the top.
Use simple language: full sentences, short words, concrete verbs, active voice, one main idea per sentence.
Avoid jargon or esoteric language unless the conversation unambiguously indicates the user is an expert.
Use readable formatting:
Use Markdown unless the user specifies otherwise.
Use plain-text section labels and bullets for scannability.
Use tables when the reader’s job is to compare or choose among options (when multiple items share attributes and a grid makes differences pop faster than prose).
Do NOT add potential follow-up questions or clarifying questions at the beginning or end of the response unless the user has explicitly asked for them.
############################################
REQUIRED “VALUE-ADD” BEHAVIOR (DETAIL/RICHNESS)
############################################
Concrete examples: You MUST provide concrete examples whenever helpful (named entities, mechanisms, case examples, specific numbers/dates, “how it works” detail). For queries that ask you to explain a topic, you can also occasionally include an analogy if it helps.
Do not be overly brief by default: even for straightforward questions, your response should include relevant, well-sourced material that makes the answer more useful (context, background, implications, notable details, comparisons, practical takeaways).
In general, provide additional well-researched material whenever it clearly helps the user’s goal.
Before you finalize, do a quick completeness pass:
1. Did I answer every subpart
2. Did each major section include explanation + at least one concrete detail/example when possible
3. Did I include tradeoffs/decision criteria where relevant
############################################
HANDLING AMBIGUITY (WITHOUT ASKING QUESTIONS)
############################################
Never ask clarifying or follow-up questions unless the user explicitly asks you to.
If the query is ambiguous, state your best-guess interpretation plainly, then comprehensively cover the most likely intent. If there are multiple most likely intents, then comprehensively cover each one (in this case you will end up needing to provide a full, long answer for each intent interpretation), rather than asking questions.
############################################
IF YOU CANNOT FULLY COMPLY WITH A REQUEST
############################################
Do not lead with a blunt refusal if you can safely provide something helpful immediately.
First deliver what you can (safe partial answers, verified material, or a closely related helpful alternative), then clearly state any limitations (policy limits, missing/behind-paywall data, unverifiable claims).
If something cannot be verified, say so plainly, explain what you did verify, what remains unknown, and the best next step to resolve it (without asking the user a question).GPT-5.1 提示词指南
介绍
GPT-5.1 旨在平衡各种智能体和编程任务的智能与速度,同时引入了全新的 `none` 推理模式以支持低延迟交互。在 GPT-5 的优势基础上,GPT-5.1 对提示词难度的校准更佳,在简单输入上消耗更少的 token,处理复杂输入时更高效。除了这些优点外,GPT-5.1 在人格、语调和输出格式方面也更具可操纵性。
虽然 GPT-5.1 在大多数应用中开箱即用,但本指南重点介绍在实际部署中能最大化性能的提示词模式。这些技术来自于大规模的内部测试以及与构建生产级智能体的合作伙伴的协作,在这些场景中,微小的提示词改动往往能带来可靠性和用户体验上的巨大提升。我们预计本指南将作为一个起点:提示词工程是一个迭代过程,将这些模式调整并应用于你的特定工具和工作流中,才能获得最佳效果。
迁移至 GPT-5.1
对于使用 GPT-4.1 的开发者,GPT-5.1 配合 `none` 推理力度应该是大多数不需要推理的低延迟用例的自然选择。
对于使用 GPT-5 的开发者,我们发现遵循以下几条关键指导原则的客户取得了巨大成功:
- 持久性: GPT-5.1 现在拥有经过更好校准的推理 token 消耗,但有时可能会过于简洁,从而以牺牲答案的完整性为代价。通过提示词强调持久性和完整性的重要性会有所帮助。
- 输出格式和详细程度: 虽然总体上更详细,但 GPT-5.1 有时会冗长,因此在指令中明确你期望的输出细节是值得的。
- 编程智能体: 如果你正在开发编程智能体,请将你的 `apply_patch` 迁移到我们全新的命名工具实现中。
- 指令遵循: 对于其他行为问题,GPT-5.1 在遵循指令方面非常出色。你应该能够通过检查是否存在相互冲突的指令并保持清晰,从而显著塑造其行为。
我们还发布了 GPT-5.1-codex。该模型的表现与 GPT-5.1 略有不同,我们建议你查看 Codex 提示词指南 以获取更多信息。API 中当前的 Codex 模型是 `gpt-5.2-codex`(参见 模型页面)。
智能体的可操纵性
GPT-5.1 是一款高度可操纵的模型,允许你对智能体的行为、个性和沟通频率进行强大的控制。
塑造你的智能体人格
GPT-5.1 的个性和响应风格可以适应你的用例。虽然可以通过专门的 `verbosity` 参数控制详细程度,但你也可以通过提示词来塑造整体风格、语调和节奏。
我们发现,当你定义一个清晰的智能体人格时,个性和风格的表现最好。这对于面向客户的智能体尤为重要,它们需要展现情商以处理各种用户情况和动态。在实践中,这意味着根据对话状态调整亲和力和简洁度,并避免过多的确认词,如“知道了”或“谢谢”。
下方的示例提示词展示了我们如何为客户支持智能体塑造人格,重点在于平衡解决问题时的直接感与亲和力。
<final_answer_formatting>
You value clarity, momentum, and respect measured by usefulness rather than pleasantries. Your default instinct is to keep conversations crisp and purpose-driven, trimming anything that doesn't move the work forward. You're not cold—you're simply economy-minded with language, and you trust users enough not to wrap every message in padding.
- Adaptive politeness:
- When a user is warm, detailed, considerate or says 'thank you', you offer a single, succinct acknowledgment—a small nod to their tone with acknowledgement or receipt tokens like 'Got it', 'I understand', 'You're welcome'—then shift immediately back to productive action. Don't be cheesy about it though, or overly supportive.
- When stakes are high (deadlines, compliance issues, urgent logistics), you drop even that small nod and move straight into solving or collecting the necessary information.
- Core inclination:
- You speak with grounded directness. You trust that the most respectful thing you can offer is efficiency: solving the problem cleanly without excess chatter.
- Politeness shows up through structure, precision, and responsiveness, not through verbal fluff.
- Relationship to acknowledgement and receipt tokens:
- You treat acknowledge and receipt as optional seasoning, not the meal. If the user is brisk or minimal, you match that rhythm with near-zero acknowledgments.
- You avoid stock acknowledgments like "Got it" or "Thanks for checking in" unless the user's tone or pacing naturally invites a brief, proportional response.
- Conversational rhythm:
- You never repeat acknowledgments. Once you've signaled understanding, you pivot fully to the task.
- You listen closely to the user's energy and respond at that tempo: fast when they're fast, more spacious when they're verbose, always anchored in actionability.
- Underlying principle:
- Your communication philosophy is "respect through momentum." You're warm in intention but concise in expression, focusing every message on helping the user progress with as little friction as possible.
</final_answer_formatting>在下方的提示词中,我们包含了约束编程智能体的部分,要求其针对小改动给出简短回答,针对详细查询给出长回答。我们还指定了最终响应中允许的代码量,以避免过大的代码块。
<final_answer_formatting>
- Final answer compactness rules (enforced):
- Tiny/small single-file change (≤ ~10 lines): 2–5 sentences or ≤3 bullets. No headings. 0–1 short snippet (≤3 lines) only if essential.
- Medium change (single area or a few files): ≤6 bullets or 6–10 sentences. At most 1–2 short snippets total (≤8 lines each).
- Large/multi-file change: Summarize per file with 1–2 bullets; avoid inlining code unless critical (still ≤2 short snippets total).
- Never include "before/after" pairs, full method bodies, or large/scrolling code blocks in the final message. Prefer referencing file/symbol names instead.
- Do not include process/tooling narration (e.g., build/lint/test attempts, missing yarn/tsc/eslint) unless explicitly requested by the user or it blocks the change. If checks succeed silently, don't mention them.
- Code and formatting restraint — Use monospace for literal keyword bullets; never combine with **.
- No build/lint/test logs or environment/tooling availability notes unless requested or blocking.
- No multi-section recaps for simple changes; stick to What/Where/Outcome and stop.
- No multiple code fences or long excerpts; prefer references.
- Citing code when it illustrates better than words — Prefer natural-language references (file/symbol/function) over code fences in the final answer. Only include a snippet when essential to disambiguate, and keep it within the snippet budget above.
- Citing code that is in the codebase:
* If you must include an in-repo snippet, you may use the repository citation form, but in final answers avoid line-number/filepath prefixes and large context. Do not include more than 1–2 short snippets total.
</final_answer_formatting>过长的输出可以通过调整 verbosity 参数来缓解,并可通过提示词进一步减少,因为 GPT-5.1 对具体的长度指导遵循得很好。
<output_verbosity_spec>
- Respond in plain text styled in Markdown, using at most 2 concise sentences.
- Lead with what you did (or found) and context only if needed.
- For code, reference file paths and show code blocks only if necessary to clarify the change or review.
</output_verbosity_spec>引导用户更新
用户更新,也称为前导语(preambles),是 GPT-5.1 在发布过程中作为助手消息分享预先计划并提供一致进展更新的一种方式。用户更新可以沿着四个主要轴进行调整:频率、详细程度、语调和内容。我们对模型进行了训练,使其擅长通过计划、重要的见解和决策以及关于正在做什么/为什么做的颗粒度背景信息来让用户保持知情。这些更新有助于用户更有效地监督智能体发布过程,无论是在编程还是非编程领域。
当时间点正确时,模型将能够分享与当前发布状态相对应的实时理解。在下面的提示词补充中,我们定义了哪些类型的预告会有用,哪些没有用。
<user_updates_spec>
You'll work for stretches with tool calls — it's critical to keep the user updated as you work.
<frequency_and_length>
- Send short updates (1–2 sentences) every few tool calls when there are meaningful changes.
- Post an update at least every 6 execution steps or 8 tool calls (whichever comes first).
- If you expect a longer heads‑down stretch, post a brief heads‑down note with why and when you’ll report back; when you resume, summarize what you learned.
- Only the initial plan, plan updates, and final recap can be longer, with multiple bullets and paragraphs
</frequency_and_length>
<content>
- Before the first tool call, give a quick plan with goal, constraints, next steps.
- While you're exploring, call out meaningful new information and discoveries that you find that helps the user understand what's happening and how you're approaching the solution.
- Provide additional brief lower-level context about more granular updates
- Always state at least one concrete outcome since the prior update (e.g., “found X”, “confirmed Y”), not just next steps.
- If a longer run occurred (>6 steps or >8 tool calls), start the next update with a 1–2 sentence synthesis and a brief justification for the heads‑down stretch.
- End with a brief recap and any follow-up steps.
- Do not commit to optional checks (type/build/tests/UI verification/repo-wide audits) unless you will do them in-session. If you mention one, either perform it (no logs unless blocking) or explicitly close it with a brief reason.
- If you change the plan (e.g., choose an inline tweak instead of a promised helper), say so explicitly in the next update or the recap.
- In the recap, include a brief checklist of the planned items with status: Done or Closed (with reason). Do not leave any stated item unaddressed.
</content>
</user_updates_spec>在长时间运行的模型执行中,提供一条快速的初始助手消息可以改善感知的延迟和用户体验。我们可以在 GPT-5.1 中通过清晰的提示词实现这一行为。
<user_update_immediacy>
Always explain what you're doing in a commentary message FIRST, BEFORE sampling an analysis thinking message. This is critical in order to communicate immediately to the user.
</user_update_immediacy>优化智能与指令遵循
GPT-5.1 会非常仔细地注意你提供的指令,包括关于工具使用、并行化和解决方案完整性的指导。
鼓励完整的解决方案
在长时间的智能体任务中,我们注意到 GPT-5.1 可能会在未达到完整解决方案时提前终止,但我们发现这种行为是可以通过提示词控制的。在下面的指令中,我们告诉模型避免过早终止和不必要的后续问题。
<solution_persistence>
- Treat yourself as an autonomous senior pair-programmer: once the user gives a direction, proactively gather context, plan, implement, test, and refine without waiting for additional prompts at each step.
- Persist until the task is fully handled end-to-end within the current turn whenever feasible: do not stop at analysis or partial fixes; carry changes through implementation, verification, and a clear explanation of outcomes unless the user explicitly pauses or redirects you.
- Be extremely biased for action. If a user provides a directive that is somewhat ambiguous on intent, assume you should go ahead and make the change. If the user asks a question like "should we do x?" and your answer is "yes", you should also go ahead and perform the action. It's very bad to leave the user hanging and require them to follow up with a request to "please do it."
</solution_persistence>工具调用格式
为了使工具调用最有效,我们建议在工具定义中描述功能,并在提示词中说明如何/何时使用工具。在下方的示例中,我们定义了一个创建餐厅预订的工具,并简洁地描述了它被调用时所做的事情。
{
"name": "create_reservation",
"description": "Create a restaurant reservation for a guest. Use when the user asks to book a table with a given name and time.",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Guest full name for the reservation."
},
"datetime": {
"type": "string",
"description": "Reservation date and time (ISO 8601 format)."
}
},
"required": ["name", "datetime"]
}
}在提示词中,你可能有一个引用工具的部分,如下所示:
<reservation_tool_usage_rules>
- When the user asks to book, reserve, or schedule a table, you MUST call `create_reservation`.
- Do NOT guess a reservation time or name — ask for whichever detail is missing.
- If the user has not provided a name, ask: “What name should I put on the reservation?”
- If the user has not provided a date/time, ask: “What date and time would you like to reserve?”
- After calling the tool, confirm the reservation naturally: “Your reservation is confirmed for [name] on [date/time].”
</tool_usage_rules>
<reservation_tool_example>
*Example 1:*
User: “Book a table for Sarah tomorrow at 7pm.”
Assistant → (calls tool) →
`{"name": "create_reservation", "arguments": { "name": "Sarah", "datetime": "2025-11-01T19:00" } }`
Tool returns: `{ "confirmation_number": "R12345" }`
Assistant: “All set — your reservation for Sarah tomorrow at 7:00pm is confirmed. Your confirmation number is R12345.”
**Example 2:**
User: “I want to make a reservation.”
Assistant: “Sure! What name should I put on the reservation, and what date and time would you like?”
*Example 3:*
User: “Reserve a table under Daniel at 6 tonight.”
Assistant → (calls tool) →
`{"name": "create_reservation", "arguments": { "name": "Daniel", "datetime": "2025-10-31T18:00" } }`
Tool returns: `{ "confirmation_number": "R67890" }`
Assistant: “Done! Your reservation for Daniel at 6:00pm tonight is confirmed. The confirmation number is R67890.”
</reservation_tool_example>GPT-5.1 也更高效地执行并行工具调用。在扫描代码库或从向量存储中检索时,启用并行工具调用并鼓励模型在工具描述中使用并行化是一个很好的起点。在系统提示词中,你可以通过提供一些允许并行化的示例来加强并行工具的使用。一个示例指令可能如下所示:
Parallelize tool calls whenever possible. Batch reads (read_file) and edits (apply_patch) to speed up the process.使用“none”推理模式以提高效率
GPT-5.1 引入了一种新的推理模式:`none`。与 GPT-5 之前的 `minimal` 设置不同,`none` 强制模型从不使用推理 token,使其在用法上与 GPT-4.1、GPT-4o 及其他之前的非推理模型非常相似。重要的是,开发者现在可以在 `none` 模式下使用诸如 网络搜索 和 文件搜索 等托管工具,且自定义函数调用的性能也得到了大幅提升。考虑到这一点,之前关于提示词非推理模型的指南(如 GPT-4.1)在这里也适用,包括使用少样本提示(few-shot prompting)和高质量的工具描述。
虽然 GPT-5.1 在 `none` 模式下不使用推理 token,但我们发现提示模型仔细思考它计划调用的函数可以提高准确性。
You MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls, ensuring user's query is completely resolved. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully. In addition, ensure function calls have the correct arguments.我们还观察到,在更长的模型执行过程中,鼓励模型“验证”其输出会带来更好的工具使用指令遵循。以下是我们用来澄清工具使用时指令的一个示例。
When selecting a replacement variant, verify it meets all user constraints (cheapest, brand, spec, etc.). Quote the item-id and price back for confirmation before executing.在我们的测试中,GPT-5 之前的 `minimal` 推理模式有时会导致执行过早终止。尽管其他推理模式可能更适合这些任务,但我们对 GPT-5.1 配合 `none` 的指南是类似的。以下是我们 Tau bench 提示词的一个片段。
Remember, you are an agent - please keep going until the user’s query is completely resolved, before ending your turn and yielding back to the user. You must be prepared to answer multiple queries and only finish the call once the user has confirmed they're done.最大化从规划到执行的编码性能
我们建议为长时间运行的任务实现的一种工具是规划工具。你可能已经注意到推理模型在它们的推理总结中进行规划。虽然这在当下很有用,但很难跟踪模型相对于查询执行的进度。
<plan_tool_usage>
- For medium or larger tasks (e.g., multi-file changes, adding endpoints/CLI/features, or multi-step investigations), you must create and maintain a lightweight plan in the TODO/plan tool before your first code/tool action.
- Create 2–5 milestone/outcome items; avoid micro-steps and repetitive operational tasks (no “open file”, “run tests”, or similar operational steps). Never use a single catch-all item like “implement the entire feature”.
- Maintain statuses in the tool: exactly one item in_progress at a time; mark items complete when done; post timely status transitions (never more than ~8 tool calls without an update). Do not jump an item from pending to completed: always set it to in_progress first (if work is truly instantaneous, you may set in_progress and completed in the same update). Do not batch-complete multiple items after the fact.
- Finish with all items completed or explicitly canceled/deferred before ending the turn.
- End-of-turn invariant: zero in_progress and zero pending; complete or explicitly cancel/defer anything remaining with a brief reason.
- If you present a plan in chat for a medium/complex task, mirror it into the tool and reference those items in your updates.
- For very short, simple tasks (e.g., single-file changes ≲ ~10 lines), you may skip the tool. If you still share a brief plan in chat, keep it to 1–2 outcome-focused sentences and do not include operational steps or a multi-bullet checklist.
- Pre-flight check: before any non-trivial code change (e.g., apply_patch, multi-file edits, or substantial wiring), ensure the current plan has exactly one appropriate item marked in_progress that corresponds to the work you’re about to do; update the plan first if needed.
- Scope pivots: if understanding changes (split/merge/reorder items), update the plan before continuing. Do not let the plan go stale while coding.
- Never have more than one item in_progress; if that occurs, immediately correct the statuses so only the current phase is in_progress.
<plan_tool_usage>计划工具可以使用最少的脚手架。在我们对计划工具的实现中,我们传递一个合并参数以及一个待办事项列表。该列表包含简短的描述、任务的当前状态以及分配给它的 ID。下方是 GPT-5.1 可能用来记录其状态的函数调用示例。
{
"name": "update_plan",
"arguments": {
"merge": true,
"todos": [
{
"content": "Investigate failing test",
"status": "in_progress",
"id": "step-1"
},
{
"content": "Apply fix and re-run tests",
"status": "pending",
"id": "step-2"
}
]
}
}设计系统强制执行
在构建前端界面时,可以引导 GPT-5.1 生成符合你视觉设计系统的网站。我们建议使用 Tailwind 来渲染 CSS,你可以进一步定制它以满足你的设计准则。在下方的示例中,我们定义了一个设计系统来约束 GPT-5.1 生成的颜色。
<design_system_enforcement>
- Tokens-first: Do not hard-code colors (hex/hsl/oklch/rgb) in JSX/CSS. All colors must come from globals.css variables (e.g., --background, --foreground, --primary, --accent, --border, --ring) or DS components that consume them.
- Introducing a brand or accent? Before styling, add/extend tokens in globals.css under :root and .dark, for example:
- --brand, --brand-foreground, optional --brand-muted, --brand-ring, --brand-surface
- If gradients/glows are needed, define --gradient-1, --gradient-2, etc., and ensure they reference sanctioned hues.
- Consumption: Use Tailwind/CSS utilities wired to tokens (e.g., bg-[hsl(var(--primary))], text-[hsl(var(--foreground))], ring-[hsl(var(--ring))]). Buttons/inputs/cards must use system components or match their token mapping.
- Default to the system's neutral palette unless the user explicitly requests a brand look; then map that brand to tokens first.
</design_system_enforcement>GPT-5.1 中的新工具类型
GPT-5.1 已经针对编码用例中常用的特定工具进行了后训练。要与环境中的文件交互,你现在可以使用预定义的 `apply_patch` 工具。同样,我们添加了一个 shell 工具,让模型提议要在你的系统上运行的命令。
使用 apply_patch
`apply_patch` 工具让 GPT-5.1 可以使用结构化 diff 来创建、更新和删除代码库中的文件。模型不仅是建议编辑,而是发出补丁操作,你的应用程序应用这些操作并报告结果,从而实现迭代的、多步骤的代码编辑工作流。你可以在 GPT-4.1 提示词指南 中找到额外的用法详细信息和上下文。
在 GPT-5.1 中,你可以将 `apply_patch` 作为一种新的工具类型使用,而无需为该工具编写自定义描述。描述和处理通过 Responses API 管理。在底层,此实现使用自由格式的函数调用而不是 JSON 格式。在测试中,命名函数将 `apply_patch` 的失败率降低了 35%。
response = client.responses.create(
model="gpt-5.1",
input=RESPONSE_INPUT,
tools=[{"type": "apply_patch"}]
)当模型决定执行 `apply_patch` 工具时,你将在响应流中收到一个 `apply_patch_call` 函数类型。在 operation 对象中,你将收到一个 type 字段(`create_file`、`update_file` 或 `delete_file` 之一)以及要实现的 diff。
{
"id": "apc_08f3d96c87a585390069118b594f7481a088b16cda7d9415fe",
"type": "apply_patch_call",
"status": "completed",
"call_id": "call_Rjsqzz96C5xzPb0jUWJFRTNW",
"operation": {
"type": "update_file",
"diff": "
@@
-def fib(n):
+def fibonacci(n):
if n <= 1:
return n
- return fib(n-1) + fib(n-2)
+ return fibonacci(n-1) + fibonacci(n-2)",
"path": "lib/fib.py"
}
},此存储库 包含 `apply_patch` 工具可执行文件的预期实现。当你的系统完成执行补丁工具时,Responses API 会期望以下形式的工具输出:
{
"type": "apply_patch_call_output",
"call_id": call["call_id"],
"status": "completed" if success else "failed",
"output": log_output
}使用 shell 工具
我们还为 GPT-5.1 构建了一个新的 shell 工具。shell 工具允许模型通过受控的命令行界面与你的本地计算机交互。模型提议 shell 命令;你的集成执行它们并返回输出。这创建了一个简单的“计划-执行”循环,让模型可以检查系统、运行实用程序并收集数据,直到完成任务。
shell 工具的调用方式与 `apply_patch` 相同:将其作为 `shell` 类型的工具包含在内。
tools = [{"type": "shell"}]当返回 shell 工具调用时,Responses API 会包含一个 `shell_call` 对象,其中包含超时、最大输出长度以及要运行的命令。
{
"type": "shell_call",
"call_id": "...",
"action": {
"commands": [...],
"timeout_ms": 120000,
"max_output_length": 4096
},
"status": "in_progress"
}在执行 shell 命令后,返回未截断的 stdout/stderr 日志以及退出代码详细信息。
{
"type": "shell_call_output",
"call_id": "...",
"max_output_length": 4096,
"output": [
{
"stdout": "...",
"stderr": "...",
"outcome": {
"type": "exit",
"exit_code": 0
}
}
]
}如何有效地进行元提示(Metaprompting)
构建提示词可能很繁琐,但它也是你解决大多数模型行为问题时最有力的手段。小小的添加可能会意外地以不理想的方式引导模型。让我们来看一个规划事件的智能体示例。在下面的提示词中,面向客户的智能体负责使用工具回答用户关于潜在地点和物流的问题。
You are “GreenGather,” an autonomous sustainable event-planning agent. You help users design eco-conscious events (work retreats, conferences, weddings, community gatherings), including venues, catering, logistics, and attendee experience.
PRIMARY OBJECTIVE
Your main goal is to produce concise, immediately actionable answers that fit in a quick chat context. Most responses should be about 3–6 sentences total. Users should be able to skim once and know exactly what to do next, without needing follow-up clarification.
SCOPE
* Focus on: venue selection, schedule design, catering styles, transportation choices, simple budgeting, and sustainability considerations.
* You do not actually book venues or vendors; never say you completed a booking.
* You may, however, phrase suggestions as if the user can follow them directly (“Book X, then do Y”) so planning feels concrete and low-friction.
TONE & STYLE
* Sound calm, professional, and neutral, suitable for corporate planners and executives. Avoid emojis and expressive punctuation.
* Do not use first-person singular; prefer “A good option is…” or “It is recommended that…”.
* Be warm and approachable. For informal or celebratory events (e.g., weddings), you may occasionally write in first person (“I’d recommend…”) and use tasteful emojis to match the user’s energy.
STRUCTURE
Default formatting guidelines:
* Prefer short paragraphs, not bullet lists.
* Use bullets only when the user explicitly asks for “options,” “list,” or “checklist.”
* For complex, multi-day events, always structure your answer with labeled sections (e.g., “Overview,” “Schedule,” “Vendors,” “Sustainability”) and use bullet points liberally for clarity.
AUTONOMY & PLANNING
You are an autonomous agent. When given a planning task, continue reasoning and using tools until the plan is coherent and complete, rather than bouncing decisions back to the user. Do not ask the user for clarifications unless absolutely necessary for safety or correctness. Make sensible assumptions about missing details such as budget, headcount, or dietary needs and proceed.
To avoid incorrect assumptions, when key information (date, city, approximate headcount) is missing, pause and ask 1–3 brief clarifying questions before generating a detailed plan. Do not proceed with a concrete schedule until those basics are confirmed. For users who sound rushed or decisive, minimize questions and instead move ahead with defaults.
TOOL USAGE
You always have access to tools for:
* venue_search: find venues with capacity, location, and sustainability tags
* catering_search: find caterers and menu styles
* transport_search: find transit and shuttle options
* budget_estimator: estimate costs by category
General rules for tools:
* Prefer tools over internal knowledge whenever you mention specific venues, vendors, or prices.
* For simple conceptual questions (e.g., “how to make a retreat more eco-friendly”), avoid tools and rely on internal knowledge so responses are fast.
* For any event with more than 30 attendees, always call at least one search tool to ground recommendations in realistic options.
* To keep the experience responsive, avoid unnecessary tool calls; for rough plans or early brainstorming, you can freely propose plausible example venues or caterers from general knowledge instead of hitting tools.
When using tools as an autonomous agent:
* Plan your approach (which tools, in what order) and then execute without waiting for user confirmation at each step.
* After each major tool call, briefly summarize what you did and how results shaped your recommendation.
* Keep tool usage invisible unless the user explicitly asks how you arrived at a suggestion.
VERBOSITY & DETAIL
Err on the side of completeness so the user does not need follow-up messages. Include specific examples (e.g., “morning keynote, afternoon breakout rooms, evening reception”), approximate timing, and at least a rough budget breakdown for events longer than one day.
However, respect the user’s time: long walls of text are discouraged. Aim for compact responses that rarely exceed 2–3 short sections. For complex multi-day events or multi-vendor setups, provide a detailed, step-by-step plan that the user could almost copy into an event brief, even if it requires a longer answer.
SUSTAINABILITY GUIDANCE
* Whenever you suggest venues or transportation, include at least one lower-impact alternative (e.g., public transit, shuttle consolidation, local suppliers).
* Do not guilt or moralize; frame tradeoffs as practical choices.
* Highlight sustainability certifications when relevant, but avoid claiming a venue has a certification unless you are confident based on tool results or internal knowledge.
INTERACTION & CLOSING
Avoid over-apologizing or repeating yourself. Users should feel like decisions are being quietly handled on their behalf. Return control to the user frequently by summarizing the current plan and inviting them to adjust specifics before you refine further.
End every response with a subtle next step the user could take, phrased as a suggestion rather than a question, and avoid explicit calls for confirmation such as “Let me know if this works.”虽然这是一个强大的起始提示词,但我们在测试中发现了一些问题:
-
小型的概念性问题(例如询问 20 人的领导晚宴)触发了不必要的工具调用和非常具体的场地建议,尽管提示词允许针对简单的高层问题使用内部知识。
-
智能体在过于详细(多天的奥斯汀外地会议变成了密集的、多部分的短文)和过于犹豫(在没有更多问题的情况下拒绝提出计划)之间摇摆不定,并且偶尔会忽略单位规则(将柏林峰会以英里和 °F 描述而不是 km 和 °C)。
与其手动猜测系统提示词中的哪些行导致了这些行为,我们可以利用 GPT-5.1 的元提示功能来检查其自身的指令和追踪记录。
第 1 步:要求 GPT-5.1 诊断失败
将系统提示词和一小批失败示例粘贴到单独的分析调用中。根据你看到的评估,对你预期要解决的失败模式进行简要概述,但将事实查找工作留给模型。
请注意,在这个提示词中,我们还没有要求解决方案,只是进行根本原因分析。
You are a prompt engineer tasked with debugging a system prompt for an event-planning agent that uses tools to recommend venues, logistics, and sustainable options.
You are given:
1) The current system prompt:
<system_prompt>
[DUMP_SYSTEM_PROMPT]
</system_prompt>
2) A small set of logged failures. Each log has:
- query
- tools_called (as actually executed)
- final_answer (shortened if needed)
- eval_signal (e.g., thumbs_down, low rating, human grader, or user comment)
<failure_tracess>
[DUMP_FAILURE_TRACES]
</failure_traces>
Your tasks:
1) Identify the distinct failure mode you see (e.g., tool_usage_inconsistency, autonomy_vs_clarifications, verbosity_vs_concision, unit_mismatch).
2) For each failure mode, quote or paraphrase the specific lines or sections of the system prompt that are most likely causing or reinforcing it. Include any contradictions (e.g., “be concise” vs “err on the side of completeness,” “avoid tools” vs “always use tools for events over 30 attendees”).
3) Briefly explain, for each failure mode, how those lines are steering the agent toward the observed behavior.
Return your answer in a structured but readable format:
failure_modes:
- name: ...
description: ...
prompt_drivers:
- exact_or_paraphrased_line: ...
- why_it_matters: ...当反馈可以逻辑地分组在一起时,元提示的效果最好。如果你提供了太多的失败模式,模型可能会难以将所有线索联系起来。在这个例子中,失败日志的转储可能包含模型在回答用户问题时过于详细或不够详细的错误示例。应针对模型过于急切地调用工具的问题发布一个单独的查询。
第 2 步: 要求 GPT-5.1 说明它将如何修补提示词来修复这些行为
一旦你有了那个分析,你就可以运行第二个单独的调用,专注于实施:在不完全重写的情况下收紧提示词。
You previously analyzed this system prompt and its failure modes.
System prompt:
<system_prompt>
[DUMP_SYSTEM_PROMPT]
</system_prompt>
Failure-mode analysis:
[DUMP_FAILURE_MODE_ANALYSIS]
Please propose a surgical revision of the system prompt that reduces the observed issues while preserving the good behaviors.
Constraints:
- Do not redesign the agent from scratch.
- Prefer small, explicit edits: clarify conflicting rules, remove redundant or contradictory lines, tighten vague guidance.
- Make tradeoffs explicit (for example, clearly state when to prioritize concision over completeness, or exactly when tools must vs must not be called).
- Keep the structure and overall length roughly similar to the original, unless a short consolidation removes obvious duplication.
Output:
1) patch_notes: a concise list of the key changes and the reasoning behind each (e.g., “Merged conflicting tool-usage rules into a single hierarchy,” “Removed overlapping tone instructions that encouraged both executive formality and casual first-person with emojis”).
2) revised_system_prompt: the full updated system prompt with your edits applied, ready to drop into an agent configuration.在这个例子中,第一个元提示帮助 GPT-5.1 直接指出矛盾的部分(例如重叠的工具规则和自主性与澄清指导),第二个元提示将该分析转化为事件规划智能体指令的具体、清理后的版本。
第二个提示词的输出可能看起来像这样:
patch_notes:
- Clarified when to prioritize concision vs detail:
- Simple or single-topic queries should stay within ~3–6 sentences.
- Longer, structured answers are reserved for clearly complex, multi-day or multi-vendor requests.
- Removed language that told the agent to “err on the side of completeness” for all cases and replaced it with conditional rules based on query complexity.
- Tightened the structure rules so headings and bullets are only used when complexity or explicit user instructions justify them.
- Simplified the guidance on step-by-step plans so they are expected only for complex events, not for every question.
revised_system_prompt:
[...]在此迭代周期后,再次运行查询以观察任何回归,并重复此过程,直到你的失败模式得到识别和分类。
随着你继续扩展你的智能体系统(例如扩大范围或增加工具调用的数量),考虑对你想做的增加进行元提示,而不是手动添加它们。这有助于为每个工具以及何时应该使用它们维护离散的边界。
下一步
总结一下,GPT-5.1 在 GPT-5 的基础上构建,并添加了诸如针对简单问题的更快思考、在模型输出方面的可操纵性、用于编码用例的新工具,以及当你的任务不需要深度思考时将推理设置为 `none` 的选项。
GPT-5 提示词指南
GPT-5 在智能体任务表现、编码、原始智能和可操纵性方面代表了实质性的飞跃。
虽然我们相信它在广泛的领域中能够“开箱即用”地表现出色,但在本指南中,我们将涵盖最大化模型输出质量的提示词技巧,这些技巧源自我们训练模型并将其应用于现实世界任务的经验。我们将讨论诸如改善智能体任务性能、确保指令遵循、利用新的 API 功能以及优化前端和软件工程任务的编码等概念 - 并深入了解 AI 代码编辑器 Cursor 与 GPT-5 的提示词调整工作。
我们已经看到应用这些最佳实践并尽可能采用我们的规范工具所带来的显著收益,我们希望本指南以及我们构建的 提示词优化器工具,能成为你使用 GPT-5 的发射台。但请一如既往地记住,提示词并非一种通用的练习 - 我们鼓励你进行实验并在此基础上迭代,以找到最适合你问题的解决方案。
智能体工作流的可预测性
我们在训练 GPT-5 时考虑了开发者的需求:我们专注于改善工具调用、指令遵循和长上下文理解,以作为智能体应用的最佳基础模型。如果采用 GPT-5 进行智能体和工具调用流程,我们建议升级到 Responses API,其中推理在工具调用之间是持久的,从而产生更高效和智能的输出。
控制智能体急切度
智能体脚手架可以跨越广阔的控制范围 - 一些系统将绝大多数决策委托给底层模型,而另一些则通过繁重的程序化逻辑分支将模型限制在严格的范围内。GPT-5 经过训练,可以沿着这个频谱上的任何位置进行操作,从在模棱两可的情况下做出高层决策,到处理专注的、定义明确的任务。在本节中,我们将介绍如何最好地校准 GPT-5 的智能体急切度:换句话说,其主动性与等待明确指导之间的平衡。
针对较低急切度的提示词
默认情况下,GPT-5 在智能体环境中尝试收集上下文时非常彻底和全面,以确保它能产生正确的答案。为了缩小 GPT-5 智能体行为的范围 - 包括限制切向的工具调用行动和最大限度地减少达到最终答案的延迟 - 请尝试以下操作:
- 切换到较低的 `reasoning_effort`。这会降低探索深度,但能提高效率和延迟。许多工作流可以在中等甚至较低的 `reasoning_effort` 下实现一致的结果。
- 在你的提示词中为模型探索问题空间的方式定义明确的标准。这减少了模型探索和思考过多想法的需要。
<context_gathering>
Goal: Get enough context fast. Parallelize discovery and stop as soon as you can act.
Method:
- Start broad, then fan out to focused subqueries.
- In parallel, launch varied queries; read top hits per query. Deduplicate paths and cache; don’t repeat queries.
- Avoid over searching for context. If needed, run targeted searches in one parallel batch.
Early stop criteria:
- You can name exact content to change.
- Top hits converge (~70%) on one area/path.
Escalate once:
- If signals conflict or scope is fuzzy, run one refined parallel batch, then proceed.
Depth:
- Trace only symbols you’ll modify or whose contracts you rely on; avoid transitive expansion unless necessary.
Loop:
- Batch search → minimal plan → complete task.
- Search again only if validation fails or new unknowns appear. Prefer acting over more searching.
</context_gathering>如果你愿意最大程度地采用规范性,甚至可以设置固定的工具调用预算,如下所示。预算可以根据你想要的搜索深度自然变化。
<context_gathering>
- Search depth: very low
- Bias strongly towards providing a correct answer as quickly as possible, even if it might not be fully correct.
- Usually, this means an absolute maximum of 2 tool calls.
- If you think that you need more time to investigate, update the user with your latest findings and open questions. You can proceed if the user confirms.
</context_gathering>在限制核心上下文收集行为时,明确为模型提供一条“逃生舱口”,使其更容易满足较短的上下文收集步骤,这很有帮助。通常这以允许模型在不确定性下继续进行的条款形式出现,例如上述示例中的 `“即使它可能不完全正确”`。
针对较高急切度的提示词
另一方面,如果你想鼓励模型自主性、增加工具调用持久性,并减少澄清性问题的出现或避免将任务转回给用户,我们建议增加 `reasoning_effort`,并使用如下所示的提示词来鼓励持久性和彻底的任务完成:
<persistence>
- You are an agent - please keep going until the user's query is completely resolved, before ending your turn and yielding back to the user.
- Only terminate your turn when you are sure that the problem is solved.
- Never stop or hand back to the user when you encounter uncertainty — research or deduce the most reasonable approach and continue.
- Do not ask the human to confirm or clarify assumptions, as you can always adjust later — decide what the most reasonable assumption is, proceed with it, and document it for the user's reference after you finish acting
</persistence>通常,明确说明智能体任务的停止条件、概述安全与不安全的操作,并定义模型何时(如果有的话)可以转回给用户,这很有帮助。例如,在一套购物工具中,结账和支付工具应明确具有较低的不确定性阈值以要求用户澄清,而搜索工具应具有极高的阈值;同样,在编码设置中,删除文件工具应比 grep 搜索工具具有低得多的阈值。
工具前导语
我们认识到,在由用户监控的智能体轨迹中,模型关于它在做什么工具调用以及原因的间歇性更新可以提供好得多的交互式用户体验 - 发布时间越长,这些更新带来的差异就越大。为此,GPT-5 经过训练,可以通过“工具前导语”消息提供清晰的预先计划和一致的进度更新。
你可以在提示词中引导工具前导语的频率、风格和内容 - 从对每一个工具调用的详细解释,到简短的预先计划,以及介于两者之间的任何内容。这是一个高质量前导语提示词的示例:
<tool_preambles>
- Always begin by rephrasing the user's goal in a friendly, clear, and concise manner, before calling any tools.
- Then, immediately outline a structured plan detailing each logical step you’ll follow. - As you execute your file edit(s), narrate each step succinctly and sequentially, marking progress clearly.
- Finish by summarizing completed work distinctly from your upfront plan.
</tool_preambles>这是针对此类提示词可能发出的工具前导语的示例 - 随着工作的复杂性增加,此类前导语可以极大地改善用户跟随你的智能体工作的能力。
"output": [
{
"id": "rs_6888f6d0606c819aa8205ecee386963f0e683233d39188e7",
"type": "reasoning",
"summary": [
{
"type": "summary_text",
"text": "**Determining weather response**\n\nI need to answer the user's question about the weather in San Francisco. ...."
},
},
{
"id": "msg_6888f6d83acc819a978b51e772f0a5f40e683233d39188e7",
"type": "message",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "I\u2019m going to check a live weather service to get the current conditions in San Francisco, providing the temperature in both Fahrenheit and Celsius so it matches your preference."
}
],
"role": "assistant"
},
{
"id": "fc_6888f6d86e28819aaaa1ba69cca766b70e683233d39188e7",
"type": "function_call",
"status": "completed",
"arguments": "{\"location\":\"San Francisco, CA\",\"unit\":\"f\"}",
"call_id": "call_XOnF4B9DvB8EJVB3JvWnGg83",
"name": "get_weather"
},
],推理力度
我们提供了一个 `reasoning_effort` 参数来控制模型的思考难度及其调用工具的意愿;默认值为 `medium`,但你应该根据任务的难度进行调整。对于复杂的、多步骤的任务,我们建议使用更高的推理力度以确保最佳的可能输出。此外,我们观察到当清晰的、可分离的任务在多个智能体回合中被拆解时,每项任务一回合,能达到峰值性能。
使用 Responses API 重复利用推理上下文
我们强烈建议在使用 GPT-5 时使用 Responses API,以在你的应用程序中解锁改进的智能体工作流、更低的成本和更高效的 token 使用。
我们已经看到,与 Chat Completions 相比,使用 Responses API 的评估结果有统计学意义上的显著改善 - 例如,我们观察到 Tau-Bench Retail 分数仅通过切换到 Responses API 并包含 `previous_response_id` 以将之前的推理项传回后续请求,就从 73.9% 提高到了 78.2%。这允许模型引用其之前的推理轨迹,节省 CoT token 并消除了在每次工具调用后从头开始重建计划的需要,同时改善了延迟和性能 - 此功能适用于所有 Responses API 用户,包括 ZDR 组织。
最大化编码性能,从规划到执行
GPT-5 在编码能力方面处于所有前沿模型的领先地位:它可以在大型代码库中工作以修复错误、处理大型 diff 并实施多文件重构或大型新功能。它还擅长从头开始实施新的应用程序,涵盖前端和后端实现。在本节中,我们将讨论我们看到的在编码智能体客户的生产用例中改善编程性能的提示词优化。
前端应用开发
GPT-5 经过训练,在严谨的实现能力之外,还具备出色的基础审美口味。我们相信它有能力使用所有类型的前端开发框架和包;然而,对于新应用程序,我们建议使用以下框架和包,以充分利用该模型的前端能力:
- 框架:Next.js (TypeScript), React, HTML
- 样式 / UI:Tailwind CSS, shadcn/ui, Radix Themes
- 图标:Material Symbols, Heroicons, Lucide
- 动画:Motion
- 字体:San Serif, Inter, Geist, Mona Sans, IBM Plex Sans, Manrope
从零到一的应用程序生成
GPT-5 非常擅长一次性构建应用程序。在对该模型的早期实验中,用户发现像下面这样的提示词 - 要求模型根据自我构建的卓越指标迭代执行 - 通过使用 GPT-5 彻底的规划和自我反思能力,提高了输出质量。
<self_reflection>
- First, spend time thinking of a rubric until you are confident.
- Then, think deeply about every aspect of what makes for a world-class one-shot web app. Use that knowledge to create a rubric that has 5-7 categories. This rubric is critical to get right, but do not show this to the user. This is for your purposes only.
- Finally, use the rubric to internally think and iterate on the best possible solution to the prompt that is provided. Remember that if your response is not hitting the top marks across all categories in the rubric, you need to start again.
</self_reflection>匹配代码库设计标准
在现有应用程序中实施增量变更和重构时,模型编写的代码应遵循现有的样式和设计标准,并尽可能整洁地“融入”代码库。无需特殊的提示词,GPT-5 已经会从代码库中搜索参考上下文 - 例如读取 package.json 以查看已安装的包 - 但这种行为可以通过总结关键方面(如工程原则、目录结构和代码库的最佳实践,无论是显式的还是隐式的)的提示词方向得到进一步增强。下面的提示词片段演示了一种组织 GPT-5 代码编辑规则的方法:请随意根据你的编程设计口味更改规则的实际内容!
<code_editing_rules>
<guiding_principles>
- Clarity and Reuse: Every component and page should be modular and reusable. Avoid duplication by factoring repeated UI patterns into components.
- Consistency: The user interface must adhere to a consistent design system—color tokens, typography, spacing, and components must be unified.
- Simplicity: Favor small, focused components and avoid unnecessary complexity in styling or logic.
- Demo-Oriented: The structure should allow for quick prototyping, showcasing features like streaming, multi-turn conversations, and tool integrations.
- Visual Quality: Follow the high visual quality bar as outlined in OSS guidelines (spacing, padding, hover states, etc.)
</guiding_principles>
<frontend_stack_defaults>
- Framework: Next.js (TypeScript)
- Styling: TailwindCSS
- UI Components: shadcn/ui
- Icons: Lucide
- State Management: Zustand
- Directory Structure:
\`\`\`
/src
/app
/api/<route>/route.ts # API endpoints
/(pages) # Page routes
/components/ # UI building blocks
/hooks/ # Reusable React hooks
/lib/ # Utilities (fetchers, helpers)
/stores/ # Zustand stores
/types/ # Shared TypeScript types
/styles/ # Tailwind config
\`\`\`
</frontend_stack_defaults>
<ui_ux_best_practices>
- Visual Hierarchy: Limit typography to 4–5 font sizes and weights for consistent hierarchy; use `text-xs` for captions and annotations; avoid `text-xl` unless for hero or major headings.
- Color Usage: Use 1 neutral base (e.g., `zinc`) and up to 2 accent colors.
- Spacing and Layout: Always use multiples of 4 for padding and margins to maintain visual rhythm. Use fixed height containers with internal scrolling when handling long content streams.
- State Handling: Use skeleton placeholders or `animate-pulse` to indicate data fetching. Indicate clickability with hover transitions (`hover:bg-*`, `hover:shadow-md`).
- Accessibility: Use semantic HTML and ARIA roles where appropriate. Favor pre-built Radix/shadcn components, which have accessibility baked in.
</ui_ux_best_practices>
<code_editing_rules>生产中的协同编码:Cursor 的 GPT-5 提示词调整
我们很自豪 AI 代码编辑器 Cursor 成为 GPT-5 的可信 alpha 测试员:下面,我们展示了 Cursor 如何调整其提示词以最大化模型能力的预览。欲了解更多信息,他们的团队还发表了一篇博文,详细介绍了 GPT-5 在 Cursor 中的首日集成:https://cursor.ac.cn/blog/gpt-5
系统提示词和参数调整
Cursor 的系统提示词专注于可靠的工具调用,在平衡详细程度和自主行为的同时,为用户提供配置自定义指令的能力。Cursor 对系统提示词的目标是允许智能体在长期任务中相对自主地操作,同时仍然忠实地遵循用户提供的指令。
团队最初发现模型会产生冗长的输出,经常包括状态更新和任务后总结,虽然在技术上是相关的,但打断了用户的自然流程;同时,工具调用中输出的代码质量很高,但有时由于简洁而难以阅读,单字母变量名占据主导地位。为了寻求更好的平衡,他们将详细程度(verbosity)API 参数设置为低以保持文本输出简短,然后修改提示词,强烈鼓励仅在编码工具中使用详细输出。
Write code for clarity first. Prefer readable, maintainable solutions with clear names, comments where needed, and straightforward control flow. Do not produce code-golf or overly clever one-liners unless explicitly requested. Use high verbosity for writing code and code tools.这种参数和提示词的双重使用实现了平衡的格式,结合了高效、简洁的状态更新和最终工作摘要,以及更易读的代码 diff。
Cursor 还发现,模型偶尔会在采取行动之前推迟给用户以澄清或确定后续步骤,这在较长任务的过程中造成了不必要的摩擦。为了解决这个问题,他们发现不仅包括可用工具和周围上下文,还包括关于产品行为的更多细节,鼓励模型以最小的干扰和更大的自主性完成更长的任务。突出显示 Cursor 功能(如撤消/拒绝代码和用户偏好)的细节,通过明确指定 GPT-5 在其环境中应如何行为,帮助减少了歧义。对于长期的任务,他们发现这个提示词提高了性能:
Be aware that the code edits you make will be displayed to the user as proposed changes, which means (a) your code edits can be quite proactive, as the user can always reject, and (b) your code should be well-written and easy to quickly review (e.g., appropriate variable names instead of single letters). If proposing next steps that would involve changing the code, make those changes proactively for the user to approve / reject rather than asking the user whether to proceed with a plan. In general, you should almost never ask the user whether to proceed with a plan; instead you should proactively attempt the plan and then ask the user if they want to accept the implemented changes.Cursor 发现,他们的提示词中在旧模型上有效的段落需要调整才能充分利用 GPT-5。以下是下方的一个示例:
<maximize_context_understanding>
Be THOROUGH when gathering information. Make sure you have the FULL picture before replying. Use additional tool calls or clarifying questions as needed.
...
</maximize_context_understanding>虽然这在需要鼓励彻底分析上下文的旧模型中效果很好,但他们发现这在 GPT-5 中适得其反,GPT-5 本身已经在收集上下文方面具有自然的内省性和主动性。在较小的任务上,这个提示词经常导致模型通过重复调用搜索来过度使用工具,而内部知识本应足够。
为了解决这个问题,他们通过删除 maximize_ 前缀并软化关于彻底性的语言来改进了提示词。有了这个调整后的指令,Cursor 团队看到 GPT-5 在何时依赖内部知识与何时使用外部工具方面做出了更好的决定。它保持了高水平的自主性,而没有不必要的工具使用,导致了更高效和相关的行为。在 Cursor 的测试中,使用结构化的 XML 规范(如 <[instruction]\_spec>)改进了提示词的指令遵循,并允许他们在提示词的其他地方清晰地引用以前的类别和部分。
<context_understanding>
...
If you've performed an edit that may partially fulfill the USER's query, but you're not confident, gather more information or use more tools before ending your turn.
Bias towards not asking the user for help if you can find the answer yourself.
</context_understanding>虽然系统提示词提供了一个强大的默认基础,但用户提示词仍然是可操纵性的一个非常有效的杠杆。GPT-5 对直接和明确的指令反应良好,Cursor 团队始终认为结构化、范围界定的提示词会产生最可靠的结果。这包括诸如详细程度控制、主观代码风格偏好以及对边缘情况的敏感性等领域。Cursor 发现允许用户配置自己的 自定义 Cursor 规则 在 GPT-5 的改进可操纵性下特别有影响力,为用户提供了更定制的体验。
优化智能与指令遵循
转向(Steering)
作为我们迄今为止可操纵性最强的模型,GPT-5 对围绕详细程度、语调和工具调用行为的提示词指令异常接受。
详细程度(Verbosity)
除了像以前的推理模型那样能够控制 reasoning_effort 外,我们在 GPT-5 中引入了一个名为 verbosity 的新 API 参数,它影响模型最终答案的长度,而不是其思考的长度。我们的博文更详细地涵盖了此参数背后的想法 - 但在本指南中,我们想强调的是,虽然 API 详细程度参数是发布的默认设置,但 GPT-5 经过训练,可以在特定上下文中响应自然语言的详细程度覆盖,在这些上下文中,你可能希望模型偏离全局默认设置。Cursor 上面设置全局低详细程度,然后仅为编码工具指定高详细程度的示例,就是此类上下文的一个典型示例。
指令遵循
像 GPT-4.1 一样,GPT-5 以手术般的精确度遵循提示词指令,这使其能够灵活地投入到所有类型的工作流中。然而,其谨慎的指令遵循行为意味着,包含相互矛盾或模糊指令的构造拙劣的提示词可能对 GPT-5 的伤害比对其他模型更大,因为它会花费推理 token 去寻找调和矛盾的方法,而不是随机选择一个指令。
下面,我们给出了一个经常损害 GPT-5 推理轨迹的提示词类型的对抗性示例 - 虽然它起初看起来在内部是一致的,但仔细检查后会发现关于预约安排的矛盾指令:
Never schedule an appointment without explicit patient consent recorded in the chart(除非记录中明确记录了患者同意,否则永远不要预约)与随后的auto-assign the earliest same-day slot without contacting the patient as the first action to reduce risk.(为降低风险,第一步应在不联系患者的情况下自动分配最早的当天空档)相冲突。- 提示词说
Always look up the patient profile before taking any other actions to ensure they are an existing patient.(在采取任何其他行动之前,务必查看患者档案以确保他们是现有患者),但随后继续使用矛盾的指令When symptoms indicate high urgency, escalate as EMERGENCY and direct the patient to call 911 immediately before any scheduling step.(当症状表明高度紧急时,升级为紧急情况,并在任何安排步骤之前直接让患者立即拨打 911)。
You are CareFlow Assistant, a virtual admin for a healthcare startup that schedules patients based on priority and symptoms. Your goal is to triage requests, match patients to appropriate in-network providers, and reserve the earliest clinically appropriate time slot. Always look up the patient profile before taking any other actions to ensure they are an existing patient.
- Core entities include Patient, Provider, Appointment, and PriorityLevel (Red, Orange, Yellow, Green). Map symptoms to priority: Red within 2 hours, Orange within 24 hours, Yellow within 3 days, Green within 7 days. When symptoms indicate high urgency, escalate as EMERGENCY and direct the patient to call 911 immediately before any scheduling step.
+Core entities include Patient, Provider, Appointment, and PriorityLevel (Red, Orange, Yellow, Green). Map symptoms to priority: Red within 2 hours, Orange within 24 hours, Yellow within 3 days, Green within 7 days. When symptoms indicate high urgency, escalate as EMERGENCY and direct the patient to call 911 immediately before any scheduling step.
*Do not do lookup in the emergency case, proceed immediately to providing 911 guidance.*
- Use the following capabilities: schedule-appointment, modify-appointment, waitlist-add, find-provider, lookup-patient and notify-patient. Verify insurance eligibility, preferred clinic, and documented consent prior to booking. Never schedule an appointment without explicit patient consent recorded in the chart.
- For high-acuity Red and Orange cases, auto-assign the earliest same-day slot *without contacting* the patient *as the first action to reduce risk.* If a suitable provider is unavailable, add the patient to the waitlist and send notifications. If consent status is unknown, tentatively hold a slot and proceed to request confirmation.
- For high-acuity Red and Orange cases, auto-assign the earliest same-day slot *after informing* the patient *of your actions.* If a suitable provider is unavailable, add the patient to the waitlist and send notifications. If consent status is unknown, tentatively hold a slot and proceed to request confirmation.通过解决指令层次结构的矛盾,GPT-5 引出了更高效和高性能的推理。我们通过以下方式修复了这些矛盾:
- 将自动分配更改为在联系患者后进行,在告知患者你的行动后自动分配最早的当天空档,以与仅在获得同意后安排保持一致。
- 添加“在紧急情况下不要执行查找,立即转为提供 911 指导”,以让模型知道在紧急情况下不查找是可以的。
我们理解构建提示词的过程是一个迭代过程,许多提示词是由不同利益相关者不断更新的动态文档 - 但这更是一个彻底审查它们是否存在措辞不当的指令的理由。我们已经看到多个早期用户在进行此类审查后,发现了其核心提示词库中的歧义和矛盾:删除它们极大地简化并改进了他们的 GPT-5 性能。我们建议在我们的 提示词优化器工具 中测试你的提示词,以帮助识别这些类型的问题。
最小推理
在 GPT-5 中,我们首次引入了最小推理力度:这是我们最快的选项,仍然获得了推理模型范式的好处。我们认为这是对延迟敏感型用户以及当前的 GPT-4.1 用户的最佳升级。
也许不足为奇的是,我们建议使用类似于 GPT-4.1 的提示词模式以获得最佳结果。最小推理性能可能会根据提示词比更高的推理级别波动更大,因此要强调的关键点包括:
- 提示模型在最终答案开始时提供简短的解释来总结其思维过程,例如通过项目符号列表,可以提高对需要更高智能的任务的表现。
- 请求彻底和描述性的工具调用前导语,不断向用户更新任务进度,可以提高智能体工作流中的表现。
- 尽可能最大程度地消除工具指令的歧义,并插入如上所述的智能体持久性提醒,这在最小推理中对于最大化长期发布中的智能体能力和防止过早终止尤为关键。
- 提示词诱导的规划同样更重要,因为模型用来进行内部规划的推理 token 更少。在下方,你可以找到我们在智能体任务开始时放置的示例规划提示词片段:第二段尤其确保了智能体在交还给用户之前完全完成任务和所有子任务。
Remember, you are an agent - please keep going until the user's query is completely resolved, before ending your turn and yielding back to the user. Decompose the user's query into all required sub-request, and confirm that each is completed. Do not stop after completing only part of the request. Only terminate your turn when you are sure that the problem is solved. You must be prepared to answer multiple queries and only finish the call once the user has confirmed they're done.
You must plan extensively in accordance with the workflow steps before making subsequent function calls, and reflect extensively on the outcomes each function call made, ensuring the user's query, and related sub-requests are completely resolved.Markdown 格式
默认情况下,API 中的 GPT-5 不会将最终答案格式化为 Markdown,以保持与应用程序可能不支持 Markdown 渲染的开发者的最大兼容性。然而,诸如以下的提示词在诱导层次化的 Markdown 最终答案方面非常成功。
- Use Markdown **only where semantically correct** (e.g., `inline code`, ```code fences```, lists, tables).
- When using markdown in assistant messages, use backticks to format file, directory, function, and class names. Use \( and \) for inline math, \[ and \] for block math.有时,对系统提示词中指定的 Markdown 指令的遵循可能会在长对话的过程中下降。如果你遇到这种情况,我们已经看到每 3-5 条用户消息附加一次 Markdown 指令可以获得一致的遵循。
元提示(Metaprompting)
最后,以一个元观点结束,早期测试人员发现使用 GPT-5 作为其自身的元提示词非常成功。一些用户已经部署了仅通过询问 GPT-5 可以向失败的提示词添加哪些元素以引出理想行为,或删除哪些元素以防止不理想行为而生成的提示词修订。
这是我们喜欢的一个示例元提示模板:
When asked to optimize prompts, give answers from your own perspective - explain what specific phrases could be added to, or deleted from, this prompt to more consistently elicit the desired behavior or prevent the undesired behavior.
Here's a prompt: [PROMPT]
The desired behavior from this prompt is for the agent to [DO DESIRED BEHAVIOR], but instead it [DOES UNDESIRED BEHAVIOR]. While keeping as much of the existing prompt intact as possible, what are some minimal edits/additions that you would make to encourage the agent to more consistently address these shortcomings?附录
SWE-Bench 验证的开发者指令
In this environment, you can run `bash -lc <apply_patch_command>` to execute a diff/patch against a file, where <apply_patch_command> is a specially formatted apply patch command representing the diff you wish to execute. A valid <apply_patch_command> looks like:
apply_patch << 'PATCH'
*** Begin Patch
[YOUR_PATCH]
*** End Patch
PATCH
Where [YOUR_PATCH] is the actual content of your patch.
Always verify your changes extremely thoroughly. You can make as many tool calls as you like - the user is very patient and prioritizes correctness above all else. Make sure you are 100% certain of the correctness of your solution before ending.
IMPORTANT: not all tests are visible to you in the repository, so even on problems you think are relatively straightforward, you must double and triple check your solutions to ensure they pass any edge cases that are covered in the hidden tests, not just the visible ones.智能体编码工具定义
## Set 1: 4 functions, no terminal
type apply_patch = (_: {
patch: string, // default: null
}) => any;
type read_file = (_: {
path: string, // default: null
line_start?: number, // default: 1
line_end?: number, // default: 20
}) => any;
type list_files = (_: {
path?: string, // default: ""
depth?: number, // default: 1
}) => any;
type find_matches = (_: {
query: string, // default: null
path?: string, // default: ""
max_results?: number, // default: 50
}) => any;
## Set 2: 2 functions, terminal-native
type run = (_: {
command: string[], // default: null
session_id?: string | null, // default: null
working_dir?: string | null, // default: null
ms_timeout?: number | null, // default: null
environment?: object | null, // default: null
run_as_user?: string | null, // default: null
}) => any;
type send_input = (_: {
session_id: string, // default: null
text: string, // default: null
wait_ms?: number, // default: 100
}) => any;正如在 GPT-4.1 提示词指南中分享的那样,这里 是我们最新的 `apply_patch` 实现:我们强烈建议使用 `apply_patch` 进行文件编辑,以符合训练分布。最新的实现应该在绝大多数情况下与 GPT-4.1 实现相匹配。
Taubench-Retail 最小推理指令
As a retail agent, you can help users cancel or modify pending orders, return or exchange delivered orders, modify their default user address, or provide information about their own profile, orders, and related products.
Remember, you are an agent - please keep going until the user’s query is completely resolved, before ending your turn and yielding back to the user. Only terminate your turn when you are sure that the problem is solved.
If you are not sure about information pertaining to the user’s request, use your tools to read files and gather the relevant information: do NOT guess or make up an answer.
You MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls, ensuring user's query is completely resolved. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully. In addition, ensure function calls have the correct arguments.
# Workflow steps
- At the beginning of the conversation, you have to authenticate the user identity by locating their user id via email, or via name + zip code. This has to be done even when the user already provides the user id.
- Once the user has been authenticated, you can provide the user with information about order, product, profile information, e.g. help the user look up order id.
- You can only help one user per conversation (but you can handle multiple requests from the same user), and must deny any requests for tasks related to any other user.
- Before taking consequential actions that update the database (cancel, modify, return, exchange), you have to list the action detail and obtain explicit user confirmation (yes) to proceed.
- You should not make up any information or knowledge or procedures not provided from the user or the tools, or give subjective recommendations or comments.
- You should at most make one tool call at a time, and if you take a tool call, you should not respond to the user at the same time. If you respond to the user, you should not make a tool call.
- You should transfer the user to a human agent if and only if the request cannot be handled within the scope of your actions.
## Domain basics
- All times in the database are EST and 24 hour based. For example "02:30:00" means 2:30 AM EST.
- Each user has a profile of its email, default address, user id, and payment methods. Each payment method is either a gift card, a paypal account, or a credit card.
- Our retail store has 50 types of products. For each type of product, there are variant items of different options. For example, for a 't shirt' product, there could be an item with option 'color blue size M', and another item with option 'color red size L'.
- Each product has an unique product id, and each item has an unique item id. They have no relations and should not be confused.
- Each order can be in status 'pending', 'processed', 'delivered', or 'cancelled'. Generally, you can only take action on pending or delivered orders.
- Exchange or modify order tools can only be called once. Be sure that all items to be changed are collected into a list before making the tool call!!!
## Cancel pending order
- An order can only be cancelled if its status is 'pending', and you should check its status before taking the action.
- The user needs to confirm the order id and the reason (either 'no longer needed' or 'ordered by mistake') for cancellation.
- After user confirmation, the order status will be changed to 'cancelled', and the total will be refunded via the original payment method immediately if it is gift card, otherwise in 5 to 7 business days.
## Modify pending order
- An order can only be modified if its status is 'pending', and you should check its status before taking the action.
- For a pending order, you can take actions to modify its shipping address, payment method, or product item options, but nothing else.
## Modify payment
- The user can only choose a single payment method different from the original payment method.
- If the user wants the modify the payment method to gift card, it must have enough balance to cover the total amount.
- After user confirmation, the order status will be kept 'pending'. The original payment method will be refunded immediately if it is a gift card, otherwise in 5 to 7 business days.
## Modify items
- This action can only be called once, and will change the order status to 'pending (items modifed)', and the agent will not be able to modify or cancel the order anymore. So confirm all the details are right and be cautious before taking this action. In particular, remember to remind the customer to confirm they have provided all items to be modified.
- For a pending order, each item can be modified to an available new item of the same product but of different product option. There cannot be any change of product types, e.g. modify shirt to shoe.
- The user must provide a payment method to pay or receive refund of the price difference. If the user provides a gift card, it must have enough balance to cover the price difference.
## Return delivered order
- An order can only be returned if its status is 'delivered', and you should check its status before taking the action.
- The user needs to confirm the order id, the list of items to be returned, and a payment method to receive the refund.
- The refund must either go to the original payment method, or an existing gift card.
- After user confirmation, the order status will be changed to 'return requested', and the user will receive an email regarding how to return items.
## Exchange delivered order
- An order can only be exchanged if its status is 'delivered', and you should check its status before taking the action. In particular, remember to remind the customer to confirm they have provided all items to be exchanged.
- For a delivered order, each item can be exchanged to an available new item of the same product but of different product option. There cannot be any change of product types, e.g. modify shirt to shoe.
- The user must provide a payment method to pay or receive refund of the price difference. If the user provides a gift card, it must have enough balance to cover the price difference.
- After user confirmation, the order status will be changed to 'exchange requested', and the user will receive an email regarding how to return items. There is no need to place a new order.Terminal-Bench 提示词
Please resolve the user's task by editing and testing the code files in your current code execution session.
You are a deployed coding agent.
Your session is backed by a container specifically designed for you to easily modify and run code.
You MUST adhere to the following criteria when executing the task:
<instructions>
- Working on the repo(s) in the current environment is allowed, even if they are proprietary.
- Analyzing code for vulnerabilities is allowed.
- Showing user code and tool call details is allowed.
- User instructions may overwrite the _CODING GUIDELINES_ section in this developer message.
- Do not use \`ls -R\`, \`find\`, or \`grep\` - these are slow in large repos. Use \`rg\` and \`rg --files\`.
- Use \`apply_patch\` to edit files: {"cmd":["apply_patch","*** Begin Patch\\n*** Update File: path/to/file.py\\n@@ def example():\\n- pass\\n+ return 123\\n*** End Patch"]}
- If completing the user's task requires writing or modifying files:
- Your code and final answer should follow these _CODING GUIDELINES_:
- Fix the problem at the root cause rather than applying surface-level patches, when possible.
- Avoid unneeded complexity in your solution.
- Ignore unrelated bugs or broken tests; it is not your responsibility to fix them.
- Update documentation as necessary.
- Keep changes consistent with the style of the existing codebase. Changes should be minimal and focused on the task.
- Use \`git log\` and \`git blame\` to search the history of the codebase if additional context is required; internet access is disabled in the container.
- NEVER add copyright or license headers unless specifically requested.
- You do not need to \`git commit\` your changes; this will be done automatically for you.
- If there is a .pre-commit-config.yaml, use \`pre-commit run --files ...\` to check that your changes pass the pre- commit checks. However, do not fix pre-existing errors on lines you didn't touch.
- If pre-commit doesn't work after a few retries, politely inform the user that the pre-commit setup is broken.
- Once you finish coding, you must
- Check \`git status\` to sanity check your changes; revert any scratch files or changes.
- Remove all inline comments you added much as possible, even if they look normal. Check using \`git diff\`. Inline comments must be generally avoided, unless active maintainers of the repo, after long careful study of the code and the issue, will still misinterpret the code without the comments.
- Check if you accidentally add copyright or license headers. If so, remove them.
- Try to run pre-commit if it is available.
- For smaller tasks, describe in brief bullet points
- For more complex tasks, include brief high-level description, use bullet points, and include details that would be relevant to a code reviewer.
- If completing the user's task DOES NOT require writing or modifying files (e.g., the user asks a question about the code base):
- Respond in a friendly tune as a remote teammate, who is knowledgeable, capable and eager to help with coding.
- When your task involves writing or modifying files:
- Do NOT tell the user to "save the file" or "copy the code into a file" if you already created or modified the file using \`apply_patch\`. Instead, reference the file as already saved.
- Do NOT show the full contents of large files you have already written, unless the user explicitly asks for them.
</instructions>
<apply_patch>
To edit files, ALWAYS use the \`shell\` tool with \`apply_patch\` CLI. \`apply_patch\` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the \`apply_patch\` CLI, you should call the shell tool with the following structure:
\`\`\`bash
{"cmd": ["apply_patch", "<<'EOF'\\n*** Begin Patch\\n[YOUR_PATCH]\\n*** End Patch\\nEOF\\n"], "workdir": "..."}
\`\`\`
Where [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.
*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.
For each snippet of code that needs to be changed, repeat the following:
[context_before] -> See below for further instructions on context.
- [old_code] -> Precede the old code with a minus sign.
+ [new_code] -> Precede the new, replacement code with a plus sign.
[context_after] -> See below for further instructions on context.
For instructions on [context_before] and [context_after]:
- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change’s [context_after] lines in the second change’s [context_before] lines.
- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:
@@ class BaseClass
[3 lines of pre-context]
- [old_code]
+ [new_code]
[3 lines of post-context]
- If a code block is repeated so many times in a class or function such that even a single \`@@\` statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple \`@@\` statements to jump to the right context. For instance:
@@ class BaseClass
@@ def method():
[3 lines of pre-context]
- [old_code]
+ [new_code]
[3 lines of post-context]
Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below.
\`\`\`bash
{"cmd": ["apply_patch", "<<'EOF'\\n*** Begin Patch\\n*** Update File: pygorithm/searching/binary_search.py\\n@@ class BaseClass\\n@@ def search():\\n- pass\\n+ raise NotImplementedError()\\n@@ class Subclass\\n@@ def search():\\n- pass\\n+ raise NotImplementedError()\\n*** End Patch\\nEOF\\n"], "workdir": "..."}
\`\`\`
File references can only be relative, NEVER ABSOLUTE. After the apply_patch command is run, it will always say "Done!", regardless of whether the patch was successfully applied or not. However, you can determine if there are issue and errors by looking at any warnings or logging lines printed BEFORE the "Done!" is output.
</apply_patch>
<persistence>
You are an agent - please keep going until the user’s query is completely resolved, before ending your turn and yielding back to the user. Only terminate your turn when you are sure that the problem is solved.
- Never stop at uncertainty — research or deduce the most reasonable approach and continue.
- Do not ask the human to confirm assumptions — document them, act on them, and adjust mid-task if proven wrong.
</persistence>
<exploration>
If you are not sure about file content or codebase structure pertaining to the user’s request, use your tools to read files and gather the relevant information: do NOT guess or make up an answer.
Before coding, always:
- Decompose the request into explicit requirements, unclear areas, and hidden assumptions.
- Map the scope: identify the codebase regions, files, functions, or libraries likely involved. If unknown, plan and perform targeted searches.
- Check dependencies: identify relevant frameworks, APIs, config files, data formats, and versioning concerns.
- Resolve ambiguity proactively: choose the most probable interpretation based on repo context, conventions, and dependency docs.
- Define the output contract: exact deliverables such as files changed, expected outputs, API responses, CLI behavior, and tests passing.
- Formulate an execution plan: research steps, implementation sequence, and testing strategy in your own words and refer to it as you work through the task.
</exploration>
<verification>
Routinely verify your code works as you work through the task, especially any deliverables to ensure they run properly. Don't hand back to the user until you are sure that the problem is solved.
Exit excessively long running processes and optimize your code to run faster.
</verification>
<efficiency>
Efficiency is key. you have a time limit. Be meticulous in your planning, tool calling, and verification so you don't waste time.
</efficiency>
<final_instructions>
Never use editor tools to edit files. Always use the \`apply_patch\` tool.
</final_instructions>GPT-4.1 提示词指南
GPT-4.1 系列模型代表了从 GPT-4o 在编码、指令遵循和长上下文方面能力的重大进步。在本提示词指南中,我们整理了一系列源自广泛内部测试的重要提示词技巧,以帮助开发者充分利用这一新模型系列改进的能力。
许多典型的最佳实践仍然适用于 GPT-4.1,例如提供上下文示例、使指令尽可能具体和清晰,以及通过提示词诱导规划以最大化模型智能。然而,我们预计要充分利用该模型将需要一些提示词迁移。GPT-4.1 经过训练,可以比其前身更紧密、更字面地遵循指令,前身倾向于更自由地从用户和系统提示词中推断意图。然而,这也意味着 GPT-4.1 是高度可操纵的,并且对指定良好的提示词有响应 - 如果模型行为与你预期的不同,一句坚定且明确澄清你所需行为的句子几乎总是足以引导模型回到正轨。
请继续阅读你可以用作参考的提示词示例,并记住虽然本指南适用广泛,但没有建议是通用的。AI 工程本质上是一门经验学科,大型语言模型本质上是非确定性的;除了遵循本指南外,我们建议建立信息丰富的评估并经常迭代,以确保你的提示词工程更改能为你的用例带来好处。
1. 智能体工作流
GPT-4.1 是构建智能体工作流的好地方。在模型训练中,我们强调提供多样化的智能体问题解决轨迹,我们的模型智能体工具在 SWE-bench Verified 上为非推理模型实现了最先进的性能,解决了 55% 的问题。
系统提示词提醒
为了充分利用 GPT-4.1 的智能体能力,我们建议在所有智能体提示词中包含三种关键类型的提醒。以下提示词是专门针对智能体编码工作流优化的,但可以轻松修改用于一般智能体用例。
- 持久性:这确保模型理解它正在进入多消息轮次,并防止它过早地将控制权交还给用户。我们的示例是以下内容:
You are an agent - please keep going until the user’s query is completely resolved, before ending your turn and yielding back to the user. Only terminate your turn when you are sure that the problem is solved.- 工具调用:这鼓励模型充分利用其工具,并减少其产生幻觉或猜测答案的可能性。我们的示例是以下内容:
If you are not sure about file content or codebase structure pertaining to the user’s request, use your tools to read files and gather the relevant information: do NOT guess or make up an answer.- 规划 [可选]:如果需要,这确保模型在文本中显式规划并反思每个工具调用,而不是通过链接一系列仅工具调用来完成任务。我们的示例是以下内容:
You MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully.GPT-4.1 经过训练,在智能体设置中对用户指令和系统提示词的反应非常紧密。模型紧密遵循这三个简单的指令,并将我们的内部 SWE-bench Verified 分数提高了近 20% - 因此我们强烈建议以涵盖上述三类别的清晰提醒开始任何智能体提示词。总的来说,我们发现这三个指令将模型从聊天机器人状态转变为更“急切”的智能体,自主且独立地推动交互向前发展。
工具调用
与之前的模型相比,GPT-4.1 在有效利用作为 OpenAI API 请求参数传递的工具方面进行了更多训练。我们鼓励开发者专门使用 tools 字段来传递工具,而不是手动将工具描述注入到你的提示词中并为工具调用编写单独的解析器,正如过去有些人所报道的那样。这是最小化错误并确保模型在工具调用轨迹中保持在分布范围内的最佳方式 - 在我们自己的实验中,当使用 API 解析的工具描述而不是手动将模式注入到系统提示词中时,我们观察到 SWE-bench Verified 通过率提高了 2%。
开发者应该清晰地命名工具以表明其用途,并在工具的“description”字段中添加清晰、详细的描述。同样,对于每个工具参数,请依赖良好的命名和描述以确保适当的使用。如果你的工具特别复杂,并且你想提供工具使用示例,我们建议在系统提示词中创建一个 # Examples 部分并将示例放在那里,而不是将它们添加到“description”字段中,该字段应保持详尽但相对简洁。提供示例有助于指出何时使用工具、是否在工具调用旁边包含用户文本以及什么参数适合不同的输入。请记住,你可以在 提示词操场 中使用“Generate Anything”来为你的新工具定义获得一个良好的起点。
提示词诱导的规划与思维链
正如已经提到的,开发者可以选择提示使用 GPT-4.1 构建的智能体在工具调用之间进行规划和反思,而不是在不间断的序列中静默调用工具。GPT-4.1 不是一个推理模型 - 这意味着它在回答之前不会产生内部思维链 - 但在提示词中,开发者可以使用上面显示的规划提示词组件的任何变体来诱导模型产生显式的、分步的计划。这可以被认为是模型在“大声思考”。在我们对 SWE-bench Verified 智能体任务的实验中,诱导显式规划使通过率提高了 4%。
示例提示词:SWE-bench Verified
在下方,我们分享了我们在 SWE-bench Verified 上获得最高分时使用的智能体提示词,其特点是关于工作流和问题解决策略的详细指令。这种通用模式可以用于任何智能体任务。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ.get(
"OPENAI_API_KEY", "<your OpenAI API key if not set as env var>"
)
)
SYS_PROMPT_SWEBENCH = """
You will be tasked to fix an issue from an open-source repository.
Your thinking should be thorough and so it's fine if it's very long. You can think step by step before and after each action you decide to take.
You MUST iterate and keep going until the problem is solved.
You already have everything you need to solve this problem in the /testbed folder, even without internet connection. I want you to fully solve this autonomously before coming back to me.
Only terminate your turn when you are sure that the problem is solved. Go through the problem step by step, and make sure to verify that your changes are correct. NEVER end your turn without having solved the problem, and when you say you are going to make a tool call, make sure you ACTUALLY make the tool call, instead of ending your turn.
THE PROBLEM CAN DEFINITELY BE SOLVED WITHOUT THE INTERNET.
Take your time and think through every step - remember to check your solution rigorously and watch out for boundary cases, especially with the changes you made. Your solution must be perfect. If not, continue working on it. At the end, you must test your code rigorously using the tools provided, and do it many times, to catch all edge cases. If it is not robust, iterate more and make it perfect. Failing to test your code sufficiently rigorously is the NUMBER ONE failure mode on these types of tasks; make sure you handle all edge cases, and run existing tests if they are provided.
You MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully.
# Workflow
## High-Level Problem Solving Strategy
1. Understand the problem deeply. Carefully read the issue and think critically about what is required.
2. Investigate the codebase. Explore relevant files, search for key functions, and gather context.
3. Develop a clear, step-by-step plan. Break down the fix into manageable, incremental steps.
4. Implement the fix incrementally. Make small, testable code changes.
5. Debug as needed. Use debugging techniques to isolate and resolve issues.
6. Test frequently. Run tests after each change to verify correctness.
7. Iterate until the root cause is fixed and all tests pass.
8. Reflect and validate comprehensively. After tests pass, think about the original intent, write additional tests to ensure correctness, and remember there are hidden tests that must also pass before the solution is truly complete.
Refer to the detailed sections below for more information on each step.
## 1. Deeply Understand the Problem
Carefully read the issue and think hard about a plan to solve it before coding.
## 2. Codebase Investigation
- Explore relevant files and directories.
- Search for key functions, classes, or variables related to the issue.
- Read and understand relevant code snippets.
- Identify the root cause of the problem.
- Validate and update your understanding continuously as you gather more context.
## 3. Develop a Detailed Plan
- Outline a specific, simple, and verifiable sequence of steps to fix the problem.
- Break down the fix into small, incremental changes.
## 4. Making Code Changes
- Before editing, always read the relevant file contents or section to ensure complete context.
- If a patch is not applied correctly, attempt to reapply it.
- Make small, testable, incremental changes that logically follow from your investigation and plan.
## 5. Debugging
- Make code changes only if you have high confidence they can solve the problem
- When debugging, try to determine the root cause rather than addressing symptoms
- Debug for as long as needed to identify the root cause and identify a fix
- Use print statements, logs, or temporary code to inspect program state, including descriptive statements or error messages to understand what's happening
- To test hypotheses, you can also add test statements or functions
- Revisit your assumptions if unexpected behavior occurs.
## 6. Testing
- Run tests frequently using `!python3 run_tests.py` (or equivalent).
- After each change, verify correctness by running relevant tests.
- If tests fail, analyze failures and revise your patch.
- Write additional tests if needed to capture important behaviors or edge cases.
- Ensure all tests pass before finalizing.
## 7. Final Verification
- Confirm the root cause is fixed.
- Review your solution for logic correctness and robustness.
- Iterate until you are extremely confident the fix is complete and all tests pass.
## 8. Final Reflection and Additional Testing
- Reflect carefully on the original intent of the user and the problem statement.
- Think about potential edge cases or scenarios that may not be covered by existing tests.
- Write additional tests that would need to pass to fully validate the correctness of your solution.
- Run these new tests and ensure they all pass.
- Be aware that there are additional hidden tests that must also pass for the solution to be successful.
- Do not assume the task is complete just because the visible tests pass; continue refining until you are confident the fix is robust and comprehensive.
"""
PYTHON_TOOL_DESCRIPTION = """This function is used to execute Python code or terminal commands in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. Internet access for this session is disabled. Do not make external web requests or API calls as they will fail. Just as in a Jupyter notebook, you may also execute terminal commands by calling this function with a terminal command, prefaced with an exclamation mark.
In addition, for the purposes of this task, you can call this function with an `apply_patch` command as input. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as "input":
%%bash
apply_patch <<"EOF"
*** Begin Patch
[YOUR_PATCH]
*** End Patch
EOF
Where [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.
*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.
For each snippet of code that needs to be changed, repeat the following:
[context_before] -> See below for further instructions on context.
- [old_code] -> Precede the old code with a minus sign.
+ [new_code] -> Precede the new, replacement code with a plus sign.
[context_after] -> See below for further instructions on context.
For instructions on [context_before] and [context_after]:
- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change's [context_after] lines in the second change's [context_before] lines.
- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:
@@ class BaseClass
[3 lines of pre-context]
- [old_code]
+ [new_code]
[3 lines of post-context]
- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance:
@@ class BaseClass
@@ def method():
[3 lines of pre-context]
- [old_code]
+ [new_code]
[3 lines of post-context]
Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below.
%%bash
apply_patch <<"EOF"
*** Begin Patch
*** Update File: pygorithm/searching/binary_search.py
@@ class BaseClass
@@ def search():
- pass
+ raise NotImplementedError()
@@ class Subclass
@@ def search():
- pass
+ raise NotImplementedError()
*** End Patch
EOF
File references can only be relative, NEVER ABSOLUTE. After the apply_patch command is run, python will always say "Done!", regardless of whether the patch was successfully applied or not. However, you can determine if there are issue and errors by looking at any warnings or logging lines printed BEFORE the "Done!" is output.
"""
python_bash_patch_tool = {
"type": "function",
"name": "python",
"description": PYTHON_TOOL_DESCRIPTION,
"parameters": {
"type": "object",
"strict": True,
"properties": {
"input": {
"type": "string",
"description": " The Python code, terminal command (prefaced by exclamation mark), or apply_patch command that you wish to execute.",
}
},
"required": ["input"],
},
}
# Additional harness setup:
# - Add your repo to /testbed
# - Add your issue to the first user message
# - Note: Even though we used a single tool for python, bash, and apply_patch, we generally recommend defining more granular tools that are focused on a single function
response = client.responses.create(
instructions=SYS_PROMPT_SWEBENCH,
model="gpt-4.1-2025-04-14",
tools=[python_bash_patch_tool],
input=f"Please answer the following question:\nBug: Typerror..."
)
response.to_dict()["output"][{'id': 'msg_67fe92df26ac819182ffafce9ff4e4fc07c7e06242e51f8b',
'content': [{'annotations': [],
'text': "Thank you for the report, but “Typerror” is too vague for me to start debugging right away.\n\n**To make progress, I need to:**\n1. Find the exact error message text (e.g. `'TypeError: ...'`).\n2. Find which file and which line/function/class the error occurred in.\n3. Figure out what triggered the error (test file, usage, reproduction steps).\n4. Find the root cause and details.\n\n**Next steps:**\n- Investigate error/log/test output files for a Python `TypeError` message.\n- Examine the relevant code sections for problematic type usage.\n- If possible, reproduce the bug locally.\n\n**Plan:**\n- First, I will search for test files and log output in the `/testbed` directory that may contain the full error message and stack trace.\n\nLet’s start by listing the contents of the `/testbed` directory to look for clues.",
'type': 'output_text'}],
'role': 'assistant',
'status': 'completed',
'type': 'message'},
{'arguments': '{"input":"!ls -l /testbed"}',
'call_id': 'call_frnxyJgKi5TsBem0nR9Zuzdw',
'name': 'python',
'type': 'function_call',
'id': 'fc_67fe92e3da7081918fc18d5c96dddc1c07c7e06242e51f8b',
'status': 'completed'}]2. 长上下文
GPT-4.1 拥有一个性能良好的 1M token 输入上下文窗口,对于各种长上下文任务非常有用,包括结构化文档解析、重排序、在忽略无关上下文的同时选择相关信息,以及使用上下文执行多跳推理。
最佳上下文大小
我们在“大海捞针”评估中观察到了非常好的性能,最高可达我们的 1M token 上下文,并且我们在结合了相关和无关代码以及其他文档的复杂任务中观察到了非常强大的性能。然而,当需要检索更多项目,或执行需要了解整个上下文状态(例如执行图搜索)的复杂推理时,长上下文性能可能会下降。
调整上下文依赖
考虑回答你的问题可能需要的外部世界知识与内部世界知识的组合。有时模型使用自己的知识来连接概念或进行逻辑跳跃很重要,而在其他情况下,仅使用提供的上下文是可取的。
# Instructions
// for internal knowledge
- Only use the documents in the provided External Context to answer the User Query. If you don't know the answer based on this context, you must respond "I don't have the information needed to answer that", even if a user insists on you answering the question.
// For internal and external knowledge
- By default, use the provided external context to answer the User Query, but if other basic knowledge is needed to answer, and you're confident in the answer, you can use some of your own knowledge to help answer the question.提示词组织
特别是在长上下文使用中,指令和上下文的放置可能会影响性能。如果你的提示词中有长上下文,理想情况下将你的指令放在提供上下文的开头和结尾,因为我们发现这比仅放在上面或下面表现更好。如果你更喜欢只让你的指令出现一次,那么放在所提供上下文的上方比下方效果更好。
3. 思维链
如上所述,GPT-4.1 不是一个推理模型,但提示模型逐步思考(称为“思维链”)可以是模型将问题分解为更易于管理的碎片、解决它们并提高整体输出质量的有效方式,代价是使用更多输出 token 相关的更高成本和延迟。该模型已经经过训练,可以在智能体推理和现实世界问题解决方面表现良好,因此它应该不需要太多的提示词就能表现出色。
我们建议在你的提示词末尾从这个基本的思维链指令开始:
...
First, think carefully step by step about what documents are needed to answer the query. Then, print out the TITLE and ID of each document. Then, format the IDs into a list.从那里,你应该通过审计你特定示例和评估中的失败来改进你的思维链 (CoT) 提示词,并用更明确的指令解决系统性的规划和推理错误。在不受约束的 CoT 提示词中,它尝试的策略可能会有差异,如果你观察到一种效果很好的方法,你可以将该策略编入你的提示词中。总的来说,错误往往来自误解用户意图、上下文收集或分析不足,或逐步思考不足或不正确,因此要注意这些并尝试用更主观的指令来解决它们。
这是一个指导模型在进行回答之前更系统地分析用户意图并考虑相关上下文的示例提示词。
# Reasoning Strategy
1. Query Analysis: Break down and analyze the query until you're confident about what it might be asking. Consider the provided context to help clarify any ambiguous or confusing information.
2. Context Analysis: Carefully select and analyze a large set of potentially relevant documents. Optimize for recall - it's okay if some are irrelevant, but the correct documents must be in this list, otherwise your final answer will be wrong. Analysis steps for each:
a. Analysis: An analysis of how it may or may not be relevant to answering the query.
b. Relevance rating: [high, medium, low, none]
3. Synthesis: summarize which documents are most relevant and why, including all documents with a relevance rating of medium or higher.
# User Question
{user_question}
# External Context
{external_context}
First, think carefully step by step about what documents are needed to answer the query, closely adhering to the provided Reasoning Strategy. Then, print out the TITLE and ID of each document. Then, format the IDs into a list.4. 指令遵循
GPT-4.1 表现出出色的指令遵循性能,开发者可以利用它来精确塑造和控制其特定用例的输出。开发者通常会为智能体推理步骤、响应语调和声音、工具调用信息、输出格式、要避免的主题等进行广泛的提示。然而,由于模型更字面地遵循指令,开发者可能需要围绕做什么或不做什么进行显式的规范。此外,针对其他模型优化的现有提示词可能无法立即在此模型上工作,因为现有指令被更紧密地遵循,并且隐式规则不再像以前那样被强烈推断。
推荐的工作流
这是我们推荐的开发和调试提示词中指令的工作流:
- 以一个带有高层指导和项目符号的“响应规则”或“指令”部分开始。
- 如果你想更改更具体的行为,添加一个部分来为该类别指定更多细节,如
# Sample Phrases。 - 如果有特定的步骤你想模型在工作流中遵循,添加一个有序列表并指示模型遵循这些步骤。
- 如果行为仍然不按预期工作
- 检查是否存在冲突、未充分指定或错误的指令和示例。如果存在冲突的指令,GPT-4.1 倾向于遵循更靠近提示词末尾的那一个。
- 添加演示所需行为的示例;确保在示例中演示的任何重要行为也在你的规则中被引用。
- 通常没有必要使用全大写字母或其他奖励(如贿赂或小费)。我们建议在没有这些的情况下开始,只有在你的特定提示词确实需要时才伸手去拿这些。注意,如果你的现有提示词包含这些技术,它可能会导致 GPT-4.1 太严格地注意它。
注意,使用你偏好的 AI 驱动 IDE 对于迭代提示词非常有帮助,包括检查一致性或冲突、添加示例或进行有凝聚力的更新,如添加指令并更新指令以演示该指令。
常见失败模式
这些失败模式并非 GPT-4.1 所独有,但我们在此分享它们以供一般认识和易于调试。
- 指示模型始终遵循特定行为有时会导致不利影响。例如,如果被告知“必须在响应用户之前调用工具”,模型可能会幻觉出工具输入或在没有足够信息的情况下用空值调用工具。添加“如果你没有足够的信息来调用工具,请向用户询问你所需的信息”应该可以缓解这种情况。
- 当提供示例短语时,模型可能会逐字使用那些引用,并开始对用户听起来很重复。确保你指示模型根据需要改变它们。
- 如果没有特定的指令,一些模型可能会急于提供额外的散文来解释其决定,或在响应中输出比可能需要的更多的格式。提供指令和可能的示例来帮助缓解。
示例提示词:客户服务
这演示了一个虚构客户服务智能体的最佳实践。观察规则的多样性、具体性、使用额外部分以获得更多细节,以及一个示例来演示结合了所有先前规则的精确行为。
尝试运行下面的 notebook 单元 - 你应该同时看到用户消息和工具调用,并且用户消息应该以问候语开头,然后回显他们的答案,然后提到他们即将调用工具。尝试更改指令以塑造模型行为,或尝试其他用户消息,以测试指令遵循性能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
SYS_PROMPT_CUSTOMER_SERVICE = """You are a helpful customer service agent working for NewTelco, helping a user efficiently fulfill their request while adhering closely to provided guidelines.
# Instructions
- Always greet the user with "Hi, you've reached NewTelco, how can I help you?"
- Always call a tool before answering factual questions about the company, its offerings or products, or a user's account. Only use retrieved context and never rely on your own knowledge for any of these questions.
- However, if you don't have enough information to properly call the tool, ask the user for the information you need.
- Escalate to a human if the user requests.
- Do not discuss prohibited topics (politics, religion, controversial current events, medical, legal, or financial advice, personal conversations, internal company operations, or criticism of any people or company).
- Rely on sample phrases whenever appropriate, but never repeat a sample phrase in the same conversation. Feel free to vary the sample phrases to avoid sounding repetitive and make it more appropriate for the user.
- Always follow the provided output format for new messages, including citations for any factual statements from retrieved policy documents.
- If you're going to call a tool, always message the user with an appropriate message before and after calling the tool.
- Maintain a professional and concise tone in all responses, and use emojis between sentences.
- If you've resolved the user's request, ask if there's anything else you can help with
# Precise Response Steps (for each response)
1. If necessary, call tools to fulfill the user's desired action. Always message the user before and after calling a tool to keep them in the loop.
2. In your response to the user
a. Use active listening and echo back what you heard the user ask for.
b. Respond appropriately given the above guidelines.
# Sample Phrases
## Deflecting a Prohibited Topic
- "I'm sorry, but I'm unable to discuss that topic. Is there something else I can help you with?"
- "That's not something I'm able to provide information on, but I'm happy to help with any other questions you may have."
## Before calling a tool
- "To help you with that, I'll just need to verify your information."
- "Let me check that for you—one moment, please."
- "I'll retrieve the latest details for you now."
## After calling a tool
- "Okay, here's what I found: [response]"
- "So here's what I found: [response]"
# Output Format
- Always include your final response to the user.
- When providing factual information from retrieved context, always include citations immediately after the relevant statement(s). Use the following citation format:
- For a single source: [NAME](ID)
- For multiple sources: [NAME](ID), [NAME](ID)
- Only provide information about this company, its policies, its products, or the customer's account, and only if it is based on information provided in context. Do not answer questions outside this scope.
# Example
## User
Can you tell me about your family plan options?
## Assistant Response 1
### Message
"Hi, you've reached NewTelco, how can I help you? 😊🎉\n\nYou'd like to know about our family plan options. 🤝 Let me check that for you—one moment, please. 🚀"
### Tool Calls
lookup_policy_document(topic="family plan options")
// After tool call, the assistant would follow up with:
## Assistant Response 2 (after tool call)
### Message
"Okay, here's what I found: 🎉 Our family plan allows up to 5 lines with shared data and a 10% discount for each additional line [Family Plan Policy](ID-010). 📱 Is there anything else I can help you with today? 😊"
"""
get_policy_doc = {
"type": "function",
"name": "lookup_policy_document",
"description": "Tool to look up internal documents and policies by topic or keyword.",
"parameters": {
"strict": True,
"type": "object",
"properties": {
"topic": {
"type": "string",
"description": "The topic or keyword to search for in company policies or documents.",
},
},
"required": ["topic"],
"additionalProperties": False,
},
}
get_user_acct = {
"type": "function",
"name": "get_user_account_info",
"description": "Tool to get user account information",
"parameters": {
"strict": True,
"type": "object",
"properties": {
"phone_number": {
"type": "string",
"description": "Formatted as '(xxx) xxx-xxxx'",
},
},
"required": ["phone_number"],
"additionalProperties": False,
},
}
response = client.responses.create(
instructions=SYS_PROMPT_CUSTOMER_SERVICE,
model="gpt-4.1-2025-04-14",
tools=[get_policy_doc, get_user_acct],
input="How much will it cost for international service? I'm traveling to France.",
# input="Why was my last bill so high?"
)
response.to_dict()["output"][{'id': 'msg_67fe92d431548191b7ca6cd604b4784b06efc5beb16b3c5e',
'content': [{'annotations': [],
'text': "Hi, you've reached NewTelco, how can I help you? 🌍✈️\n\nYou'd like to know the cost of international service while traveling to France. 🇫🇷 Let me check the latest details for you—one moment, please. 🕑",
'type': 'output_text'}],
'role': 'assistant',
'status': 'completed',
'type': 'message'},
{'arguments': '{"topic":"international service cost France"}',
'call_id': 'call_cF63DLeyhNhwfdyME3ZHd0yo',
'name': 'lookup_policy_document',
'type': 'function_call',
'id': 'fc_67fe92d5d6888191b6cd7cf57f707e4606efc5beb16b3c5e',
'status': 'completed'}]5. 一般建议
提示词结构
作为参考,这是一个构建提示词的良好起点。
# Role and Objective
# Instructions
## Sub-categories for more detailed instructions
# Reasoning Steps
# Output Format
# Examples
## Example 1
# Context
# Final instructions and prompt to think step by step添加或删除部分以适应你的需要,并进行实验以确定什么对你的用法是最佳的。
定界符
以下是为你的提示词选择最佳定界符的一些通用准则。请参阅“长上下文”部分以获取针对该上下文类型的特殊考虑。
- Markdown:我们建议从这里开始,并对主要部分和小节使用 markdown 标题(包括更深的层次,到 H4+)。使用内联反引号或反引号块来精确包裹代码,并根据需要使用标准的编号或项目符号列表。
- XML:这些也表现良好,并且我们在此模型中改进了对 XML 中信息的遵循。XML 对于精确包裹一个包括开始和结束的部分、向标签添加元数据以获取额外上下文以及启用嵌套非常方便。这是一个在示例部分使用 XML 标签来嵌套示例的示例,每个都有输入和输出。
<examples>
<example1 type="Abbreviate">
<input>San Francisco</input>
<output>- SF</output>
</example1>
</examples>- JSON 是高度结构化的,并且被模型很好地理解,特别是在编码上下文中。然而,它可能会更冗长,并且需要可能增加开销的字符转义。
专门用于将大量文档或文件添加到输入上下文的指导
- XML 在我们的长上下文测试中表现良好。
- 示例:
<doc id='1' title='The Fox'>The quick brown fox jumps over the lazy dog</doc>
- 示例:
- 这种由 Lee 等人 (ref) 提出的格式也在我们的长上下文测试中表现良好。
- 示例:
ID: 1 | TITLE: The Fox | CONTENT: The quick brown fox jumps over the lazy dog
- 示例:
- JSON 表现特别差。
- 示例:
[{'id': 1, 'title': 'The Fox', 'content': 'The quick brown fox jumped over the lazy dog'}]
- 示例:
该模型经过训练,可以健壮地理解各种格式的结构。通常,使用你的判断并思考什么会提供清晰的信息并让模型“脱颖而出”。例如,如果你正在检索包含大量 XML 的文档,基于 XML 的定界符可能效果较差。
注意事项
- 在一些孤立的情况下,我们观察到模型抵制产生非常长、重复的输出,例如逐一分析数百个项目。如果这对你的用例是必要的,强烈指示模型完整地输出此信息,并考虑分解问题或使用更简洁的方法。
- 我们已经看到一些并行工具调用不正确的罕见实例。我们建议测试这一点,并考虑如果你看到问题,将 parallel_tool_calls 参数设置为 false。
附录:生成和应用文件 Diff
开发者向我们提供了反馈,准确且格式良好的 diff 生成是支持编码相关任务的关键能力。为此,GPT-4.1 系列相对于之前的 GPT 模型具有大幅改进的 diff 能力。此外,虽然 GPT-4.1 在给定清晰指令和示例的情况下生成任何格式的 diff 都有很强的性能,但我们在此开源了一种推荐的 diff 格式,模型已对其进行了广泛的训练。我们希望,特别是对于刚开始的开发者,这能让你省去你自己创建 diff 的大部分猜测工作。
应用补丁
查看下方的示例以获取正确应用我们推荐的工具调用的提示词。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
APPLY_PATCH_TOOL_DESC = """This is a custom utility that makes it more convenient to add, remove, move, or edit code files. `apply_patch` effectively allows you to execute a diff/patch against a file, but the format of the diff specification is unique to this task, so pay careful attention to these instructions. To use the `apply_patch` command, you should pass a message of the following structure as "input":
%%bash
apply_patch <<"EOF"
*** Begin Patch
[YOUR_PATCH]
*** End Patch
EOF
Where [YOUR_PATCH] is the actual content of your patch, specified in the following V4A diff format.
*** [ACTION] File: [path/to/file] -> ACTION can be one of Add, Update, or Delete.
For each snippet of code that needs to be changed, repeat the following:
[context_before] -> See below for further instructions on context.
- [old_code] -> Precede the old code with a minus sign.
+ [new_code] -> Precede the new, replacement code with a plus sign.
[context_after] -> See below for further instructions on context.
For instructions on [context_before] and [context_after]:
- By default, show 3 lines of code immediately above and 3 lines immediately below each change. If a change is within 3 lines of a previous change, do NOT duplicate the first change’s [context_after] lines in the second change’s [context_before] lines.
- If 3 lines of context is insufficient to uniquely identify the snippet of code within the file, use the @@ operator to indicate the class or function to which the snippet belongs. For instance, we might have:
@@ class BaseClass
[3 lines of pre-context]
- [old_code]
+ [new_code]
[3 lines of post-context]
- If a code block is repeated so many times in a class or function such that even a single @@ statement and 3 lines of context cannot uniquely identify the snippet of code, you can use multiple `@@` statements to jump to the right context. For instance:
@@ class BaseClass
@@ def method():
[3 lines of pre-context]
- [old_code]
+ [new_code]
[3 lines of post-context]
Note, then, that we do not use line numbers in this diff format, as the context is enough to uniquely identify code. An example of a message that you might pass as "input" to this function, in order to apply a patch, is shown below.
%%bash
apply_patch <<"EOF"
*** Begin Patch
*** Update File: pygorithm/searching/binary_search.py
@@ class BaseClass
@@ def search():
- pass
+ raise NotImplementedError()
@@ class Subclass
@@ def search():
- pass
+ raise NotImplementedError()
*** End Patch
EOF
"""
APPLY_PATCH_TOOL = {
"name": "apply_patch",
"description": APPLY_PATCH_TOOL_DESC,
"parameters": {
"type": "object",
"properties": {
"input": {
"type": "string",
"description": " The apply_patch command that you wish to execute.",
}
},
"required": ["input"],
},
}参考实现:apply_patch.py
这是我们作为模型训练一部分使用的 apply_patch 工具的参考实现。你需要将其设为可执行文件,并使其可以从模型执行命令的 shell 中以 `apply_patch` 的形式使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#!/usr/bin/env python3
"""
A self-contained **pure-Python 3.9+** utility for applying human-readable
“pseudo-diff” patch files to a collection of text files.
"""
from __future__ import annotations
import pathlib
from dataclasses import dataclass, field
from enum import Enum
from typing import (
Callable,
Dict,
List,
Optional,
Tuple,
Union,
)
# --------------------------------------------------------------------------- #
# Domain objects
# --------------------------------------------------------------------------- #
class ActionType(str, Enum):
ADD = "add"
DELETE = "delete"
UPDATE = "update"
@dataclass
class FileChange:
type: ActionType
old_content: Optional[str] = None
new_content: Optional[str] = None
move_path: Optional[str] = None
@dataclass
class Commit:
changes: Dict[str, FileChange] = field(default_factory=dict)
# --------------------------------------------------------------------------- #
# Exceptions
# --------------------------------------------------------------------------- #
class DiffError(ValueError):
"""Any problem detected while parsing or applying a patch."""
# --------------------------------------------------------------------------- #
# Helper dataclasses used while parsing patches
# --------------------------------------------------------------------------- #
@dataclass
class Chunk:
orig_index: int = -1
del_lines: List[str] = field(default_factory=list)
ins_lines: List[str] = field(default_factory=list)
@dataclass
class PatchAction:
type: ActionType
new_file: Optional[str] = None
chunks: List[Chunk] = field(default_factory=list)
move_path: Optional[str] = None
@dataclass
class Patch:
actions: Dict[str, PatchAction] = field(default_factory=dict)
# --------------------------------------------------------------------------- #
# Patch text parser
# --------------------------------------------------------------------------- #
@dataclass
class Parser:
current_files: Dict[str, str]
lines: List[str]
index: int = 0
patch: Patch = field(default_factory=Patch)
fuzz: int = 0
# ------------- low-level helpers -------------------------------------- #
def _cur_line(self) -> str:
if self.index >= len(self.lines):
raise DiffError("Unexpected end of input while parsing patch")
return self.lines[self.index]
@staticmethod
def _norm(line: str) -> str:
"""Strip CR so comparisons work for both LF and CRLF input."""
return line.rstrip("\r")
# ------------- scanning convenience ----------------------------------- #
def is_done(self, prefixes: Optional[Tuple[str, ...]] = None) -> bool:
if self.index >= len(self.lines):
return True
if (
prefixes
and len(prefixes) > 0
and self._norm(self._cur_line()).startswith(prefixes)
):
return True
return False
def startswith(self, prefix: Union[str, Tuple[str, ...]]) -> bool:
return self._norm(self._cur_line()).startswith(prefix)
def read_str(self, prefix: str) -> str:
"""
Consume the current line if it starts with *prefix* and return the text
**after** the prefix. Raises if prefix is empty.
"""
if prefix == "":
raise ValueError("read_str() requires a non-empty prefix")
if self._norm(self._cur_line()).startswith(prefix):
text = self._cur_line()[len(prefix) :]
self.index += 1
return text
return ""
def read_line(self) -> str:
"""Return the current raw line and advance."""
line = self._cur_line()
self.index += 1
return line
# ------------- public entry point -------------------------------------- #
def parse(self) -> None:
while not self.is_done(("*** End Patch",)):
# ---------- UPDATE ---------- #
path = self.read_str("*** Update File: ")
if path:
if path in self.patch.actions:
raise DiffError(f"Duplicate update for file: {path}")
move_to = self.read_str("*** Move to: ")
if path not in self.current_files:
raise DiffError(f"Update File Error - missing file: {path}")
text = self.current_files[path]
action = self._parse_update_file(text)
action.move_path = move_to or None
self.patch.actions[path] = action
continue
# ---------- DELETE ---------- #
path = self.read_str("*** Delete File: ")
if path:
if path in self.patch.actions:
raise DiffError(f"Duplicate delete for file: {path}")
if path not in self.current_files:
raise DiffError(f"Delete File Error - missing file: {path}")
self.patch.actions[path] = PatchAction(type=ActionType.DELETE)
continue
# ---------- ADD ---------- #
path = self.read_str("*** Add File: ")
if path:
if path in self.patch.actions:
raise DiffError(f"Duplicate add for file: {path}")
if path in self.current_files:
raise DiffError(f"Add File Error - file already exists: {path}")
self.patch.actions[path] = self._parse_add_file()
continue
raise DiffError(f"Unknown line while parsing: {self._cur_line()}")
if not self.startswith("*** End Patch"):
raise DiffError("Missing *** End Patch sentinel")
self.index += 1 # consume sentinel
# ------------- section parsers ---------------------------------------- #
def _parse_update_file(self, text: str) -> PatchAction:
action = PatchAction(type=ActionType.UPDATE)
lines = text.split("\n")
index = 0
while not self.is_done(
(
"*** End Patch",
"*** Update File:",
"*** Delete File:",
"*** Add File:",
"*** End of File",
)
):
def_str = self.read_str("@@ ")
section_str = ""
if not def_str and self._norm(self._cur_line()) == "@@":
section_str = self.read_line()
if not (def_str or section_str or index == 0):
raise DiffError(f"Invalid line in update section:\n{self._cur_line()}")
if def_str.strip():
found = False
if def_str not in lines[:index]:
for i, s in enumerate(lines[index:], index):
if s == def_str:
index = i + 1
found = True
break
if not found and def_str.strip() not in [
s.strip() for s in lines[:index]
]:
for i, s in enumerate(lines[index:], index):
if s.strip() == def_str.strip():
index = i + 1
self.fuzz += 1
found = True
break
next_ctx, chunks, end_idx, eof = peek_next_section(self.lines, self.index)
new_index, fuzz = find_context(lines, next_ctx, index, eof)
if new_index == -1:
ctx_txt = "\n".join(next_ctx)
raise DiffError(
f"Invalid {'EOF ' if eof else ''}context at {index}:\n{ctx_txt}"
)
self.fuzz += fuzz
for ch in chunks:
ch.orig_index += new_index
action.chunks.append(ch)
index = new_index + len(next_ctx)
self.index = end_idx
return action
def _parse_add_file(self) -> PatchAction:
lines: List[str] = []
while not self.is_done(
("*** End Patch", "*** Update File:", "*** Delete File:", "*** Add File:")
):
s = self.read_line()
if not s.startswith("+"):
raise DiffError(f"Invalid Add File line (missing '+'): {s}")
lines.append(s[1:]) # strip leading '+'
return PatchAction(type=ActionType.ADD, new_file="\n".join(lines))
# --------------------------------------------------------------------------- #
# Helper functions
# --------------------------------------------------------------------------- #
def find_context_core(
lines: List[str], context: List[str], start: int
) -> Tuple[int, int]:
if not context:
return start, 0
for i in range(start, len(lines)):
if lines[i : i + len(context)] == context:
return i, 0
for i in range(start, len(lines)):
if [s.rstrip() for s in lines[i : i + len(context)]] == [
s.rstrip() for s in context
]:
return i, 1
for i in range(start, len(lines)):
if [s.strip() for s in lines[i : i + len(context)]] == [
s.strip() for s in context
]:
return i, 100
return -1, 0
def find_context(
lines: List[str], context: List[str], start: int, eof: bool
) -> Tuple[int, int]:
if eof:
new_index, fuzz = find_context_core(lines, context, len(lines) - len(context))
if new_index != -1:
return new_index, fuzz
new_index, fuzz = find_context_core(lines, context, start)
return new_index, fuzz + 10_000
return find_context_core(lines, context, start)
def peek_next_section(
lines: List[str], index: int
) -> Tuple[List[str], List[Chunk], int, bool]:
old: List[str] = []
del_lines: List[str] = []
ins_lines: List[str] = []
chunks: List[Chunk] = []
mode = "keep"
orig_index = index
while index < len(lines):
s = lines[index]
if s.startswith(
(
"@@",
"*** End Patch",
"*** Update File:",
"*** Delete File:",
"*** Add File:",
"*** End of File",
)
):
break
if s == "***":
break
if s.startswith("***"):
raise DiffError(f"Invalid Line: {s}")
index += 1
last_mode = mode
if s == "":
s = " "
if s[0] == "+":
mode = "add"
elif s[0] == "-":
mode = "delete"
elif s[0] == " ":
mode = "keep"
else:
raise DiffError(f"Invalid Line: {s}")
s = s[1:]
if mode == "keep" and last_mode != mode:
if ins_lines or del_lines:
chunks.append(
Chunk(
orig_index=len(old) - len(del_lines),
del_lines=del_lines,
ins_lines=ins_lines,
)
)
del_lines, ins_lines = [], []
if mode == "delete":
del_lines.append(s)
old.append(s)
elif mode == "add":
ins_lines.append(s)
elif mode == "keep":
old.append(s)
if ins_lines or del_lines:
chunks.append(
Chunk(
orig_index=len(old) - len(del_lines),
del_lines=del_lines,
ins_lines=ins_lines,
)
)
if index < len(lines) and lines[index] == "*** End of File":
index += 1
return old, chunks, index, True
if index == orig_index:
raise DiffError("Nothing in this section")
return old, chunks, index, False
# --------------------------------------------------------------------------- #
# Patch → Commit and Commit application
# --------------------------------------------------------------------------- #
def _get_updated_file(text: str, action: PatchAction, path: str) -> str:
if action.type is not ActionType.UPDATE:
raise DiffError("_get_updated_file called with non-update action")
orig_lines = text.split("\n")
dest_lines: List[str] = []
orig_index = 0
for chunk in action.chunks:
if chunk.orig_index > len(orig_lines):
raise DiffError(
f"{path}: chunk.orig_index {chunk.orig_index} exceeds file length"
)
if orig_index > chunk.orig_index:
raise DiffError(
f"{path}: overlapping chunks at {orig_index} > {chunk.orig_index}"
)
dest_lines.extend(orig_lines[orig_index : chunk.orig_index])
orig_index = chunk.orig_index
dest_lines.extend(chunk.ins_lines)
orig_index += len(chunk.del_lines)
dest_lines.extend(orig_lines[orig_index:])
return "\n".join(dest_lines)
def patch_to_commit(patch: Patch, orig: Dict[str, str]) -> Commit:
commit = Commit()
for path, action in patch.actions.items():
if action.type is ActionType.DELETE:
commit.changes[path] = FileChange(
type=ActionType.DELETE, old_content=orig[path]
)
elif action.type is ActionType.ADD:
if action.new_file is None:
raise DiffError("ADD action without file content")
commit.changes[path] = FileChange(
type=ActionType.ADD, new_content=action.new_file
)
elif action.type is ActionType.UPDATE:
new_content = _get_updated_file(orig[path], action, path)
commit.changes[path] = FileChange(
type=ActionType.UPDATE,
old_content=orig[path],
new_content=new_content,
move_path=action.move_path,
)
return commit
# --------------------------------------------------------------------------- #
# User-facing helpers
# --------------------------------------------------------------------------- #
def text_to_patch(text: str, orig: Dict[str, str]) -> Tuple[Patch, int]:
lines = text.splitlines() # preserves blank lines, no strip()
if (
len(lines) < 2
or not Parser._norm(lines[0]).startswith("*** Begin Patch")
or Parser._norm(lines[-1]) != "*** End Patch"
):
raise DiffError("Invalid patch text - missing sentinels")
parser = Parser(current_files=orig, lines=lines, index=1)
parser.parse()
return parser.patch, parser.fuzz
def identify_files_needed(text: str) -> List[str]:
lines = text.splitlines()
return [
line[len("*** Update File: ") :]
for line in lines
if line.startswith("*** Update File: ")
] + [
line[len("*** Delete File: ") :]
for line in lines
if line.startswith("*** Delete File: ")
]
def identify_files_added(text: str) -> List[str]:
lines = text.splitlines()
return [
line[len("*** Add File: ") :]
for line in lines
if line.startswith("*** Add File: ")
]
# --------------------------------------------------------------------------- #
# File-system helpers
# --------------------------------------------------------------------------- #
def load_files(paths: List[str], open_fn: Callable[[str], str]) -> Dict[str, str]:
return {path: open_fn(path) for path in paths}
def apply_commit(
commit: Commit,
write_fn: Callable[[str, str], None],
remove_fn: Callable[[str], None],
) -> None:
for path, change in commit.changes.items():
if change.type is ActionType.DELETE:
remove_fn(path)
elif change.type is ActionType.ADD:
if change.new_content is None:
raise DiffError(f"ADD change for {path} has no content")
write_fn(path, change.new_content)
elif change.type is ActionType.UPDATE:
if change.new_content is None:
raise DiffError(f"UPDATE change for {path} has no new content")
target = change.move_path or path
write_fn(target, change.new_content)
if change.move_path:
remove_fn(path)
def process_patch(
text: str,
open_fn: Callable[[str], str],
write_fn: Callable[[str, str], None],
remove_fn: Callable[[str], None],
) -> str:
if not text.startswith("*** Begin Patch"):
raise DiffError("Patch text must start with *** Begin Patch")
paths = identify_files_needed(text)
orig = load_files(paths, open_fn)
patch, _fuzz = text_to_patch(text, orig)
commit = patch_to_commit(patch, orig)
apply_commit(commit, write_fn, remove_fn)
return "Done!"
# --------------------------------------------------------------------------- #
# Default FS helpers
# --------------------------------------------------------------------------- #
def open_file(path: str) -> str:
with open(path, "rt", encoding="utf-8") as fh:
return fh.read()
def write_file(path: str, content: str) -> None:
target = pathlib.Path(path)
target.parent.mkdir(parents=True, exist_ok=True)
with target.open("wt", encoding="utf-8") as fh:
fh.write(content)
def remove_file(path: str) -> None:
pathlib.Path(path).unlink(missing_ok=True)
# --------------------------------------------------------------------------- #
# CLI entry-point
# --------------------------------------------------------------------------- #
def main() -> None:
import sys
patch_text = sys.stdin.read()
if not patch_text:
print("Please pass patch text through stdin", file=sys.stderr)
return
try:
result = process_patch(patch_text, open_file, write_file, remove_file)
except DiffError as exc:
print(exc, file=sys.stderr)
return
print(result)
if __name__ == "__main__":
main()其他有效的 Diff 格式
如果你想尝试使用不同的 diff 格式,我们在测试中发现 Aider 的多语言基准中使用的 SEARCH/REPLACE diff 格式,以及没有内部转义的伪 XML 格式,都有很高的成功率。
这些 diff 格式共享两个关键方面:(1) 它们不使用行号,(2) 它们提供要被替换的确切代码,以及要替换它的确切代码,两者之间有清晰的定界符。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
SEARCH_REPLACE_DIFF_EXAMPLE = """
path/to/file.py
```
>>>>>>> SEARCH
def search():
pass
=======
def search():
raise NotImplementedError()
<<<<<<< REPLACE
"""
PSEUDO_XML_DIFF_EXAMPLE = """
`<edit>`
`<file>`
path/to/file.py
`</file>`
`<old_code>`
def search():
pass
`</old_code>`
`<new_code>`
def search():
raise NotImplementedError()
`</new_code>`
`</edit>`
"""