Skip to content

Shorten cold starts by loading MCP helpers and the RunState validator on demand - #6221

Merged
markstuart-oai merged 1 commit into
mainfrom
perf/reduce-agents-cold-startup
Nov 25, 2026
Merged

markstuart-oai merged 1 commit into
mainfrom
perf/reduce-agents-cold-startup

Conversation

@markstuart-oai

@markstuart-oai markstuart-oai commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This pull request has been cutting cold starts by 25% for a local function tool. From process launch through two model turns, one tool call and a checked answer, latency has been dropping from 1,154 ms to 857 ms.

On main, import agents has been loading the Model Context Protocol (MCP) client and server and building a Pydantic validator for the union of response input types. Even a new run with no MCP server or saved state has been paying both costs.

With this change, the SDK has been doing that work on demand.

  • agents.mcp.util — loading the MCP helpers during tool conversion, invocation or custom data extraction.
  • RunState — building the input validator on the first restore of a handoff or compaction item. Later restores have been reusing only the completed validator. No shared run data or change to the saved format.

Benchmarks

Fresh processes with warm file and bytecode caches. OpenAI Python 3.48.0 and Pydantic 2.14.3. Each row below has been using matched process pairs in both orders. The Responses client has been sending two requests to a mock HTTP transport. No live inference.

Elapsed time Pairs Main, ms PR, ms Paired change [95% interval]
import agents only 40 1,028 764 −26.0% [−27.2, −24.0]
Launch to local tool and answer 32 1,154 857 −25.2% [−27.5, −24.2]
Launch to answer with the Responses client 32 1,284 904 −29.4% [−30.4, −28.3]
Installed wheel, no extras, local tool, MCP 1.30.0 16 985 692 −29.7% [−30.4, −28.1]
Installed wheel, no extras, local tool, MCP 2.3.0 16 1,190 667 −44.3% [−45.0, −43.6]
Launch with MCPServerStdio imported, local tool and answer 32 1,120 1,060 −6.4% [−7.5, −4.8]

Peak memory through the first answer with the Responses client has been falling by 20.4 MiB. In the main benchmark environment, the SDK has been loading 324 fewer modules.

Why both changes? Across 24 matched groups, the MCP change from #6216 alone has been saving 229 ms per first answer. Deferring the validator as well has been saving another 28 ms. In four separate traces of the MCP-only change, a full garbage collection has been adding about 100 ms to the first typed stream after large outputs. With both changes, that stream has been taking 8 ms, as on main. Other sequences have been hitting pauses on different calls.

First-use costs. Importing and configuring a stdio server after a plain request has been taking 229 ms longer. Launch through both steps has been taking 52 ms less. Restoring a saved handoff and compaction for the first time has been taking 32 ms, up from 4 ms. Launch through the resumed answer has been taking 238 ms less. Both steps have been running synchronously, with a risk of pausing the event loop.

Slower requests.

  • For typed text after large integer lists, the median of three runs has been rising from 6.58 to 7.38 ms (+11.6%). A smaller collection has been moving from the first call to the second. The three-call total has been falling from 28.9 to 21.6 ms.
  • For 32 concurrent streams, the second batch has been taking 464 ms, up from 376 ms. A full collection has been landing inside this batch. Across the three timed batches, +1.1% [−0.1, +2.6]. Across 12 batches including setup and checks, −3.6% [−5.0, −1.2] in 24 separate pairs.
  • With 24 ordinary typed calls followed by 24 streams, the second stream has been taking 72 ms, up from 9 ms. The sum across 48 requests has been falling from 586 to 478 ms.
  • Large integer-list output has been taking 2.6% longer [+1.9, +3.2]. For the small SQLite write, +1.8% [−3.9, +5.7]. Both intervals have been crossing the 3% margin.

Separate SQLite check. In 128 groups of four concurrent writers, two per version, the small write has been changing by +0.18% [−3.20, +1.81] and the large write by +0.01% [−0.40, +0.12]. All committed payloads verified. These synchronized runs have been keeping both changes within the margin, with no pooling into the 83 results below.

The two startup rows in the full table have been using the same timed child and transcript checks on both versions. Only the check after timing for the third-party mcp import has been differing. The empty custom server has been listing tools on both turns without loading mcp on the PR. Separate controls with explicit MCPServer and MCPServerStdio imports have been requiring the package on both sides.

Regression checks across all 83 comparisons with Codex off. Offline Python SDK workloads on one Linux host with CPython 3.12.16. This change alone against main at 4e162c79, without network or model latency.

95% interval versus a 3% slowdown Within margin Uncertain Above margin
Initial, all 83 77 5 1
Independent repeat, 7 selected 4 2 1
Latest result per workload 80 2 1
Timings and 95% intervals for all 83 comparisons
  • Initial — 32 matched pairs of fresh processes, balanced by run order.
  • Repeat — 64 new pairs for every initial upper bound above +3%, both SQLite writes and 32 concurrent streams. No pooling.
  • Main / PR — median elapsed time in milliseconds, from the repeat where available. For repeated calls, the median across process medians. For 32 streams, the median of three timed batches.
  • Change — median within-pair percentage change, with 95% bounds from 5,000 resamples of whole pairs. Negative for faster. Calculated from individual pairs before taking the median.
  • Margin — within for an upper bound ≤ +3%, above for a lower bound > +3%, otherwise uncertain. A result within the margin can include a small slowdown. Individual intervals only, without correction for multiple comparisons.
Workload Main, ms PR, ms Initial % [95% interval] Repeat % [95% interval]
Fresh-agent control, normal 40.315 37.208 -1.76 [-4.96, +3.80] -7.69 [-8.70, -6.33]
Fresh-agent control, streaming 291.43 176.55 -37.94 [-39.60, -36.29] —
Integer-list output, 17 calls, 32769 initial entries, normal 235.86 242.73 +2.03 [+0.72, +3.15] +2.59 [+1.86, +3.18]
Integer-list output, 17 calls, 32769 initial entries, streaming 263.35 264.48 +0.69 [-1.24, +1.88] —
Integer-list output, 3 calls, 8193 initial entries, normal 13.203 13.098 -0.30 [-1.62, +0.09] —
Integer-list output, 3 calls, 8193 initial entries, streaming 16.099 15.577 -2.59 [-5.22, -0.73] —
Reused-agent control, normal 37.642 37.605 +0.50 [-0.80, +1.78] —
Reused-agent control, streaming 39.26 39.885 +1.08 [-0.84, +2.71] —
Typed text-list output, 3 calls, 257 initial entries, normal 6.5757 7.3755 +14.88 [+11.24, +17.66] +11.56 [+10.47, +13.82]
Typed text-list output, 3 calls, 257 initial entries, streaming 8.0598 8.0708 +0.39 [-0.87, +1.60] —
Dynamic tool addition 1.7113 1.7033 -0.80 [-1.22, -0.22] —
Agent with 12 tools, dictionary settings, construction only 0.051345 0.05145 +0.30 [-0.22, +0.90] —
Agent with 12 tools, default settings, construction only 0.069823 0.069602 -0.47 [-0.85, -0.29] —
Agent with 12 tools, typed settings, construction only 0.038298 0.038365 +0.25 [-0.46, +0.50] —
Concurrent runs, 1 task 0.50685 0.47938 -5.22 [-6.03, -4.42] —
Concurrent runs, 32 tasks 12.146 12.223 -0.31 [-2.81, +1.00] —
Concurrent runs, 8 tasks 2.8917 2.8634 -1.56 [-2.60, -0.71] —
32 concurrent runs with GC observation 12.09 12.001 -0.99 [-1.89, +0.63] —
Stream completion, 128 history messages 1.2552 1.2206 -2.94 [-3.59, -2.49] —
0 available handoffs 0.5002 0.48872 -2.90 [-4.34, -0.61] —
12 available handoffs 0.71077 0.70387 -1.23 [-1.67, -0.99] —
Input history, 120 messages 0.92343 0.89528 -2.69 [-4.00, -2.10] —
Input history, 2000 messages 6.115 6.1351 +0.29 [-0.82, +1.34] —
Input history, 720 messages 2.6488 2.6143 -1.22 [-1.87, -0.62] —
6 handoff routes, mixed agent names 0.72768 0.71489 -1.60 [-2.40, -1.08] —
Structured streamed response, 67 fields, 16 tool turns 74.531 74.531 +0.21 [-1.31, +1.40] —
8 handoff routes, repeated agent names 0.99223 0.98403 -1.04 [-1.38, -0.36] —
Retained history, 16384 existing + 1 new message 0.40211 0.40509 +0.69 [-2.24, +2.76] —
Retained history, 2048 existing + 1 new message 0.05341 0.053776 +0.23 [-0.34, +1.00] —
Agent with 6 tools, dictionary settings, construction + run 0.77637 0.76769 -0.99 [-1.49, -0.44] —
Agent with 6 tools, default settings, construction + run 0.82495 0.81434 -1.38 [-1.79, -1.03] —
Agent with 6 tools, typed settings, construction + run 0.74726 0.73262 -1.80 [-2.16, -1.60] —
24 available tool schemas 0.84994 0.84071 -0.98 [-1.60, +0.12] —
Server-managed conversation, 1 history message 1.8157 1.7912 -1.45 [-1.83, -0.83] —
Server-managed conversation, 1024 history messages 34.842 35.002 +0.10 [-1.51, +2.15] —
Server-managed conversation, 128 history messages 4.6636 4.6185 -0.59 [-1.52, +0.68] —
Server-managed conversation, 512 history messages 15.278 15.099 -2.24 [-3.66, +1.80] —
0 handoff routes, distinct agent names 0.49659 0.48829 -2.07 [-2.92, -1.16] —
12 handoff routes, distinct agent names 0.59851 0.59214 -1.05 [-1.92, -0.27] —
Streamed final response 0.78291 0.75572 -4.17 [-4.80, -3.39] —
32 sustained concurrent runs 11.949 11.745 -0.66 [-1.31, -0.16] —
SQLite committed batch, 1024 items × 4096 payload characters 59.927 58.752 +9.32 [+2.71, +13.65] +0.76 [-2.00, +2.75]
Retained history, 16384 existing + 2 new messages 0.80625 0.81117 +1.21 [+0.51, +1.74] —
Retained history, 2048 existing + 2 new messages 0.10828 0.10551 -2.11 [-3.18, -0.90] —
SQLite committed batch, 2 items × 256 payload characters 7.2814 7.5386 -1.12 [-4.38, +5.08] +1.78 [-3.87, +5.67]
Retained history, 4096 existing + 8 new messages 0.83406 0.83498 +0.07 [-0.96, +0.85] —
Tool loop, 1 turn 1.6083 1.5836 -2.74 [-3.16, -1.22] —
Tool loop, 16 turns 19.248 19.152 -0.68 [-1.34, +0.74] —
Tool loop, 4 turns 4.6254 4.5547 -1.68 [-2.51, -1.01] —
Warm final response 0.48545 0.47386 -2.33 [-4.32, -1.32] —
Tool-turn repetition control, 1 call, 1024 history messages 36.071 35.156 -2.84 [-3.95, -1.28] —
Tool-turn repetition control, 2 calls, 32 history messages 3.7817 3.7501 -0.68 [-1.18, -0.03] —
Tool turn, 12 calls, 64 history messages 16.853 16.676 -0.90 [-1.76, -0.30] —
Tool turn, 1 call, 1024 history messages 34.957 35.078 +0.52 [-0.49, +1.31] —
Tool turn, 1 call, 2048 history messages 93.562 93.846 +0.43 [-0.48, +1.09] —
Tool turn, 1 call, 256 history messages 7.8216 7.748 -0.79 [-1.53, +0.04] —
Structured tool workload, 4 calls, 1024 history messages 60.114 59.771 +0.01 [-1.53, +1.69] —
Structured tool workload, 8 calls, 128 history messages 38.067 37.943 +0.29 [-1.97, +1.53] —
32 concurrent streams, 2 tool calls, 128 history messages 484.99 460.96 -5.66 [-6.53, -4.41] -4.95 [-5.61, -3.53]
First completed run, 1 tool turn, including launch 1153.8 856.69 -25.22 [-27.50, -24.18] —
First completed run with empty tool server, 1 tool turn, including launch 1194.8 936.14 -24.59 [-26.79, -20.54] —
List output, 1 call × 1024 entries 5.8725 5.819 -0.82 [-2.12, +0.64] —
List output, 8 calls × 8192 entries 58.711 58.679 +0.24 [-1.56, +2.20] —
Provider-response conversion, 1 call, 2048 history messages 315.81 317.15 +0.35 [-0.29, +1.07] —
Provider-response conversion, 1 call, 4096 history messages 749.58 740.29 -0.75 [-1.95, +0.63] —
Line-item calculation, 1 call × 1 record 1.7381 1.7149 -1.40 [-1.91, -0.72] —
Line-item calculation, 1 call × 64 records 2.6407 2.6112 -1.25 [-1.77, -0.54] —
Line-item calculation, 8 calls × 16 records 11.705 11.553 -0.97 [-2.08, -0.69] —
Line-item calculation, 8 calls × 256 records 37.313 37.352 -0.06 [-0.80, +0.24] —
Line-item calculation, 8 calls × 64 records 17.056 16.874 -1.24 [-1.89, -0.91] —
Structured response, 12 tool calls, 96 fields, normal 35.783 35.131 -1.71 [-2.52, +1.07] —
Structured response, 12 tool calls, 96 fields, stream 57.817 57.012 -1.66 [-4.09, -0.58] —
Structured response, 4 tool calls, 12 fields, stream 11.73 11.52 -1.62 [-3.77, -1.11] —
Structured response, 8 tool calls, 0 fields, stream 14.588 14.363 -1.59 [-2.40, -0.54] —
Sustained structured tool workload, 4 calls, 1024 history messages 59.641 59.215 -0.59 [-1.57, +0.16] —
Integer list, 1 call × 1 entry 1.7196 1.7064 -0.37 [-1.06, +0.05] —
Integer list, 8 calls × 8192 entries 33.586 33.147 -0.98 [-1.81, -0.11] —
Mixed list, 4 calls × 512 entries 10.603 10.347 -0.55 [-1.74, +1.08] —
Record list, 8 calls × 512 entries 13.316 13.138 -1.10 [-1.38, -0.47] —
Typed text list, 16 calls × 1024 entries 81.984 82.207 +2.60 [+1.38, +5.69] +0.42 [-0.31, +1.74]
Typed text list, 4 calls × 128 entries 7.2176 7.0857 -1.24 [-2.41, -0.46] —
Warm tool control, 1 call, 0 history messages 1.7436 1.7068 -2.01 [-2.34, -1.39] —
Warm tool control, 1 call, 4096 history messages 35.78 35.626 -0.77 [-2.09, +1.21] —

Test plan and checks

  • 838 focused tests, including a local tool call without importing MCP and a saved handoff and compaction round trip. Separate approval, replay and concurrent restore checks on Pydantic 2.12.2 and 2.14.3.
  • Full repository verifier (.agents/skills/code-change-verification/scripts/run.sh) including formatting, lint, mypy, Pyright and both pytest phases. 12,866 passed, 30 skipped.
  • Independent review of the complete diff. Identical Git tree for the local checks and PR head.

@markstuart-oai
markstuart-oai requested review from a team, rm-openai and seratch as code owners November 25, 2026 08:09
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T08:10:23.599006Z 39b6e68 PR opened
🔒 Security Review ✅ Completed 2026-11-25T08:10:58.728498Z 39b6e68 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@jbeckwith-oai jbeckwith-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved after comprehensive static review and independent review. The deferred MCP imports cover each use site, and the cached response-input adapter retains the same validation and restoration fallback behavior. No per-run state is shared by the cache. No actionable findings.

Hosted CI: 24 successful checks on this commit. No candidate code, local tests, or benchmarks were executed.

@markstuart-oai
markstuart-oai added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 7acce4f Nov 25, 2026
26 checks passed
@markstuart-oai
markstuart-oai deleted the perf/reduce-agents-cold-startup branch November 25, 2026 20:20
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.

2 participants