Skip to content

feat: add on_turn_end callback for inter-turn state changes#3589

Open
Oxygen56 wants to merge 1 commit into
openai:mainfrom
Oxygen56:feat/turn-end-callback-2671
Open

feat: add on_turn_end callback for inter-turn state changes#3589
Oxygen56 wants to merge 1 commit into
openai:mainfrom
Oxygen56:feat/turn-end-callback-2671

Conversation

@Oxygen56
Copy link
Copy Markdown

@Oxygen56 Oxygen56 commented Jun 5, 2026

Adds on_turn_end callback for inspecting/modifying agent state between turns.

Changes

RunHooksBase and AgentHooksBase (lifecycle.py)

  • Added on_turn_end lifecycle hook method that fires after each turn completes

RunConfig (run_config.py)

  • Added TurnEndData dataclass carrying the current agent, run context, and turn number
  • Added OnTurnEndCallback type alias for the callback signature
  • Added on_turn_end field to RunConfig as an optional callback

Turn loop wiring (run.py, run_internal/run_loop.py)

  • Invokes on_turn_end at all turn boundaries in both the standard and streaming execution paths:
    • NextStepHandoff (after handoff, before next agent starts)
    • NextStepRunAgain (after tool execution, before next model call)

Use cases

  • Logging and state tracking between turns
  • Dynamic instruction updates based on completed turn context
  • Context compaction in long-running workflows
  • Intercepting new external input between turns

Closes #2671

🤖 Generated with Claude Code

Adds on_turn_end callback to RunHooksBase, AgentHooksBase, and RunConfig,
firing after each agent turn to allow state inspection and dynamic
modification. The callback is invoked at all turn boundaries (handoffs
and run-again steps) in both the standard and streaming execution paths.

- RunHooksBase.on_turn_end: lifecycle hook for run-level turn completion
- AgentHooksBase.on_turn_end: per-agent lifecycle hook for turn completion
- RunConfig.on_turn_end: standalone callback via TurnEndData
- TurnEndData: dataclass carrying agent, context, and turn number

Closes openai#2671
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6c17b31eac

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/run_config.py
For example, you can use this to add a system prompt to the input.
"""

on_turn_end: OnTurnEndCallback | None = None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Append the RunConfig callback to preserve positional calls

Adding on_turn_end here shifts every existing public RunConfig positional argument after call_model_input_filter: callers that currently pass tool_error_formatter, session_settings, reasoning_item_id_policy, sandbox, or later fields positionally will now bind those values to the wrong field. This violates the repo's public API positional-compatibility rule for exported dataclasses and breaks existing patterns covered by tests/test_source_compat_constructors.py; append the new optional field after the current fields or add a compatibility layer.

Useful? React with 👍 / 👎.

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.

Feature request: better support for agent state changes between turns

1 participant