Skip to content

feat: GPT tool-use steering + strip budget warnings from history#3479

Closed
teknium1 wants to merge 1 commit intomainfrom
feat/gpt-tool-steering
Closed

feat: GPT tool-use steering + strip budget warnings from history#3479
teknium1 wants to merge 1 commit intomainfrom
feat/gpt-tool-steering

Conversation

@teknium1
Copy link
Copy Markdown
Contributor

Summary

Two changes to improve tool reliability, especially for OpenAI GPT models:

1. GPT tool-use enforcement prompt

Adds GPT_TOOL_USE_GUIDANCE to the system prompt when the model name contains gpt and tools are loaded. This addresses a known behavioral pattern where GPT models describe intended actions ("I will run the tests", "Next time I reply, it'll be with concrete results") instead of actually making tool calls.

Both OpenCode and Cline have extensive model-specific steering for this exact issue:

  • OpenCode's beast.txt: "when you say you are going to make a tool call, make sure you ACTUALLY make the tool call, instead of ending your turn"
  • Cline's GPT-5.1 variant: "You always respond using tools. They must be used as the only method of responding."

Our guidance is injected in _build_system_prompt() when "gpt" in self.model.lower() and the model has tools loaded. It's part of the frozen system prompt, so no cache-breaking concerns.

2. Budget warning history stripping

Budget pressure warnings injected by _get_budget_warning() into tool result content (e.g. [BUDGET WARNING: ... Provide your final response NOW. No more tool calls unless absolutely critical.]) now get stripped when conversation history is replayed via run_conversation().

Previously these turn-scoped signals persisted in the session transcript. When the gateway replayed history for the next user message, the model would see old warnings telling it not to use tools — and comply, avoiding tool calls in ALL subsequent turns.

The fix strips both JSON _budget_warning keys and plain-text budget warning patterns from tool-result messages at the start of run_conversation().

Files changed

  • agent/prompt_builder.py — New GPT_TOOL_USE_GUIDANCE constant
  • run_agent.py — Import + inject guidance in _build_system_prompt(), new _strip_budget_warnings_from_history() function + call in run_conversation()
  • tests/agent/test_prompt_builder.py — 8 new tests (guidance content + budget stripping)

Test plan

  • python -m pytest tests/agent/test_prompt_builder.py -n0 -q → 106 passed
  • python -m pytest tests/test_run_agent.py -n0 -q → 201 passed

Two changes to improve tool reliability, especially for OpenAI GPT models:

1. GPT tool-use enforcement prompt: Adds GPT_TOOL_USE_GUIDANCE to the
   system prompt when the model name contains 'gpt' and tools are loaded.
   This addresses a known behavioral pattern where GPT models describe
   intended actions ('I will run the tests') instead of actually making
   tool calls. Inspired by similar steering in OpenCode (beast.txt) and
   Cline (GPT-5.1 variant).

2. Budget warning history stripping: Budget pressure warnings injected by
   _get_budget_warning() into tool results are now stripped when
   conversation history is replayed via run_conversation(). Previously,
   these turn-scoped signals persisted across turns, causing models to
   avoid tool calls in all subsequent messages after any turn that hit
   the 70-90% iteration threshold.
teknium1 added a commit that referenced this pull request Mar 28, 2026
Cherry-pick of feat/gpt-tool-steering with modifications:

1. Tool-use enforcement prompt (refactored from GPT-specific):
   - Renamed GPT_TOOL_USE_GUIDANCE -> TOOL_USE_ENFORCEMENT_GUIDANCE
   - Added TOOL_USE_ENFORCEMENT_MODELS tuple: ('gpt', 'codex')
   - Injection logic now checks against the tuple instead of hardcoding
     'gpt' — adding new model families is a one-line change
   - Addresses models describing actions instead of making tool calls

2. Budget warning history stripping:
   - _strip_budget_warnings_from_history() strips _budget_warning JSON
     keys and [BUDGET WARNING: ...] text from tool results at the start
     of run_conversation()
   - Prevents old budget warnings from poisoning subsequent turns

Based on PR #3479 by teknium1.
teknium1 added a commit that referenced this pull request Mar 28, 2026
Cherry-pick of feat/gpt-tool-steering with modifications:

1. Tool-use enforcement prompt (refactored from GPT-specific):
   - Renamed GPT_TOOL_USE_GUIDANCE -> TOOL_USE_ENFORCEMENT_GUIDANCE
   - Added TOOL_USE_ENFORCEMENT_MODELS tuple: ('gpt', 'codex')
   - Injection logic now checks against the tuple instead of hardcoding
     'gpt' — adding new model families is a one-line change
   - Addresses models describing actions instead of making tool calls

2. Budget warning history stripping:
   - _strip_budget_warnings_from_history() strips _budget_warning JSON
     keys and [BUDGET WARNING: ...] text from tool results at the start
     of run_conversation()
   - Prevents old budget warnings from poisoning subsequent turns

Based on PR #3479 by teknium1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant