Skip to content

Commit fc36aef

Browse files
feat(api): set and read agent session spending limits (#4315)
## Summary Support for `spend_control` in synchronous and asynchronous beta agent sessions, including streaming creation. ## Changes - Set a best-effort limit in USD cents. On update, omit `spend_control` to keep the limit or clear it with a null limit. - Read `limit` and `consumed` on every session. Both keys are required in response fixtures. A null limit means unlimited spending. Null consumption means the amount is unavailable. - The `budget_reached` status, `session_budget_exceeded` turn error and typed `AgentSessionBudgetReachedEvent`. ## Validation Local checks have been passing: 581 focused agent tests with Pydantic v2, 493 with Pydantic v1, whole-tree Ruff, and whole-tree type checking. Coverage has been exercising exact nullable spending values, omitted and null updates, and typed budget events through synchronous and asynchronous clients. Co-authored-by: markstuart-oai <323302876+markstuart-oai@users.noreply.github.com>
1 parent f63f14b commit fc36aef

15 files changed

Lines changed: 357 additions & 18 deletions

File tree

‎.castiron.stats.yml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
schema_version: 1
2-
generation_id: 84cadef0-3ff5-45fb-be96-81e72f1f1c23
3-
openapi_spec_hash: 2b8a518f116ba253bf51f42979adab09
4-
openapi_transformed_spec_hash: b738fc05e7d9b281857dc656fe33aa42
5-
config_hash: 94ef47b9aa904df37859861d724c30d6
6-
codegen_sha: 55baaeaebaa4dfebc414857f2332911902284f7e
7-
codegen_hash: ac598006ba02ff7d7f12e87ed2ef4be76041545648262fc95def80b1d8deb3f9
2+
generation_id: 6cad12fe-8061-4bcc-bd32-8eb430ff2cff
3+
openapi_spec_hash: 93534c83c91cc99ee20f61f008c691e2
4+
openapi_transformed_spec_hash: 3544408783aa4062227cbe0ae6363a41
5+
config_hash: 98b84a99474d4d011c1aaa92435f067a
6+
codegen_sha: 3e54a1be1a8799a9ce3719eab2968a7bdcc35c23
7+
codegen_hash: 06c2cbc793c40c202f75dfb71c3ca38d51fa35fee2c742f593cf78fdf2e4739f

‎api.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ from openai.types.beta import (
550550
AgentSendSubagentInputCallItem,
551551
AgentSession,
552552
AgentSessionAssistantMessage,
553+
AgentSessionBudgetReachedEvent,
553554
AgentSessionCreatedEvent,
554555
AgentSessionDeleted,
555556
AgentSessionEnvironmentConnectedEvent,

‎api_reference/openapi.transformed.yml‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91517,6 +91517,7 @@ components:
9151791517
enum:
9151891518
- context_length_exceeded
9151991519
- max_output_tokens
91520+
- session_budget_exceeded
9152091521
- usage_limit_exceeded
9152191522
- project_spend_limit_exceeded
9152291523
- organization_spend_limit_exceeded
@@ -91541,6 +91542,7 @@ components:
9154191542
x-enumDescriptions:
9154291543
- The request exceeds the model's context window.
9154391544
- The model reached its output token limit before finishing the response.
91545+
- The session has reached its usage budget.
9154491546
- The organization has reached a usage, plan, or billing limit.
9154591547
- The project has reached its enforced spend limit.
9154691548
- The organization has reached its enforced spend limit.
@@ -93047,11 +93049,13 @@ components:
9304793049
- in_progress
9304893050
- requires_action
9304993051
- failed
93052+
- budget_reached
9305093053
x-enumDescriptions:
9305193054
- The session has no turn in progress and is ready for input. A hosted environment may still be provisioning.
9305293055
- The session is processing a turn.
9305393056
- The session is waiting for one or more required actions.
9305493057
- The session failed.
93058+
- The session is paused because its spending limit was reached.
9305593059
description: The current status of a session.
9305693060
McpApprovalRequestResourceMcpToolCall:
9305793061
type: object
@@ -93502,6 +93506,70 @@ components:
9350293506
- $ref: '#/components/schemas/EnvironmentResourceNone'
9350393507
- $ref: '#/components/schemas/EnvironmentResourceOpenaiHosted'
9350493508
- $ref: '#/components/schemas/EnvironmentResourceSelfHosted'
93509+
SessionBudgetControlResource:
93510+
type: object
93511+
properties:
93512+
limit:
93513+
type:
93514+
- integer
93515+
- 'null'
93516+
format: int64
93517+
minimum: 1
93518+
maximum: 4503599627370495
93519+
description: The configured positive limit in USD cents, or null when unlimited.
93520+
consumed:
93521+
type:
93522+
- integer
93523+
- 'null'
93524+
minimum: 0
93525+
description: Best-effort recorded spend floored to whole USD cents, or null when unavailable.
93526+
required:
93527+
- limit
93528+
- consumed
93529+
additionalProperties: false
93530+
description: Session-wide spending limit and best-effort consumption, including unlimited sessions.
93531+
SessionBudgetControlParams:
93532+
type: object
93533+
properties:
93534+
limit:
93535+
type:
93536+
- integer
93537+
- 'null'
93538+
format: int64
93539+
minimum: 1
93540+
maximum: 4503599627370495
93541+
description: Positive USD cents, or null to remove the limit.
93542+
required:
93543+
- limit
93544+
additionalProperties: false
93545+
description: Sets the session-wide limit without changing recorded spend.
93546+
SessionEventAgentSessionBudgetReached:
93547+
type: object
93548+
properties:
93549+
type:
93550+
type: string
93551+
enum:
93552+
- agent.session.budget_reached
93553+
default: agent.session.budget_reached
93554+
x-stainless-const: true
93555+
description: The type of the object. Always `agent.session.budget_reached`.
93556+
event_id:
93557+
type: string
93558+
minLength: 0
93559+
description: The unique ID of the event.
93560+
session:
93561+
$ref: '#/components/schemas/SessionResource'
93562+
description: The paused session.
93563+
obfuscation:
93564+
type: string
93565+
minLength: 0
93566+
description: Random alphanumeric padding. Present when stream obfuscation is enabled.
93567+
required:
93568+
- type
93569+
- event_id
93570+
- session
93571+
additionalProperties: false
93572+
description: Emitted when a session pauses because its spending limit was reached.
9350593573
SessionResource:
9350693574
type: object
9350793575
properties:
@@ -93513,6 +93581,9 @@ components:
9351393581
review:
9351493582
$ref: '#/components/schemas/SessionReviewResource'
9351593583
description: Review applied to eligible actions in this session. Omitted when review is none and automatic review is not enabled. Omission means none.
93584+
spend_control:
93585+
$ref: '#/components/schemas/SessionBudgetControlResource'
93586+
description: Spending limit and best-effort consumption, in USD cents. The limit is null for an unlimited session. Consumption is null when unavailable.
9351693587
metadata:
9351793588
type: object
9351893589
additionalProperties:
@@ -93578,6 +93649,7 @@ components:
9357893649
- type: 'null'
9357993650
description: Best-effort token usage for the session, or null if unknown. Recorded usage may change.
9358093651
required:
93652+
- spend_control
9358193653
- metadata
9358293654
- id
9358393655
- object
@@ -93684,6 +93756,11 @@ components:
9368493756
review:
9368593757
$ref: '#/components/schemas/SessionReviewParam'
9368693758
description: Action review mode. Omit to use none.
93759+
spend_control:
93760+
anyOf:
93761+
- $ref: '#/components/schemas/SessionBudgetControlParams'
93762+
- type: 'null'
93763+
description: Optional spending limit in USD cents. Omission or null creates an unlimited session.
9368793764
metadata:
9368893765
type:
9368993766
- object
@@ -95360,6 +95437,7 @@ components:
9536095437
agent.session.turn.cancelled: '#/components/schemas/SessionEventAgentSessionTurnCancelled'
9536195438
agent.session.turn.item.added: '#/components/schemas/SessionEventAgentSessionTurnItemAdded'
9536295439
agent.session.idle: '#/components/schemas/SessionEventAgentSessionIdle'
95440+
agent.session.budget_reached: '#/components/schemas/SessionEventAgentSessionBudgetReached'
9536395441
agent.session.in_progress: '#/components/schemas/SessionEventAgentSessionInProgress'
9536495442
agent.session.requires_action: '#/components/schemas/SessionEventAgentSessionRequiresAction'
9536595443
agent.session.failed: '#/components/schemas/SessionEventAgentSessionFailed'
@@ -95391,6 +95469,7 @@ components:
9539195469
- agent.session.turn.cancelled
9539295470
- agent.session.turn.item.added
9539395471
- agent.session.idle
95472+
- agent.session.budget_reached
9539495473
- agent.session.in_progress
9539595474
- agent.session.requires_action
9539695475
- agent.session.failed
@@ -95425,6 +95504,7 @@ components:
9542595504
- $ref: '#/components/schemas/SessionEventAgentSessionTurnCancelled'
9542695505
- $ref: '#/components/schemas/SessionEventAgentSessionTurnItemAdded'
9542795506
- $ref: '#/components/schemas/SessionEventAgentSessionIdle'
95507+
- $ref: '#/components/schemas/SessionEventAgentSessionBudgetReached'
9542895508
- $ref: '#/components/schemas/SessionEventAgentSessionInProgress'
9542995509
- $ref: '#/components/schemas/SessionEventAgentSessionRequiresAction'
9543095510
- $ref: '#/components/schemas/SessionEventAgentSessionFailed'
@@ -95494,6 +95574,11 @@ components:
9549495574
review:
9549595575
$ref: '#/components/schemas/SessionReviewParam'
9549695576
description: Default action review for new root turns. Existing turns and their subagents keep their review mode.
95577+
spend_control:
95578+
anyOf:
95579+
- $ref: '#/components/schemas/SessionBudgetControlParams'
95580+
- type: 'null'
95581+
description: Omit to retain the limit; null or a null limit removes it without resetting spend.
9549795582
agent:
9549895583
$ref: '#/components/schemas/UpdateSessionAgentParam'
9549995584
description: Agent settings to update on this session. Omitted fields stay unchanged.

‎scripts/castiron/generated-files.json‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": {
44
"api.md": {
55
"mode": "100644",
6-
"sha256": "9b8076604c5fa4ab278804b9893edfed16206861f4f93e67e7a4dc503f132c5d"
6+
"sha256": "6474a963c5d360eb6e63a3e4c4b305a0d2508afcc85a730a552df8666fe658f3"
77
},
88
"src/openai/__init__.py": {
99
"mode": "100644",
@@ -303,7 +303,7 @@
303303
},
304304
"src/openai/resources/beta/agents/sessions/sessions.py": {
305305
"mode": "100644",
306-
"sha256": "7f4393866730e2d79a254c46559561e36674de90e102c4a3ff516fd89b8d6109"
306+
"sha256": "d7b1d7b3e97f28f73634ed36ab83a2fdc9bf3a2c1c7e85e4607cca49f841db5a"
307307
},
308308
"src/openai/resources/beta/agents/sessions/subagents/__init__.py": {
309309
"mode": "100644",
@@ -1659,7 +1659,7 @@
16591659
},
16601660
"src/openai/types/beta/__init__.py": {
16611661
"mode": "100644",
1662-
"sha256": "7db7f38a689e6256dae3ff5227510e053f915a5782454b3adde9095264afb499"
1662+
"sha256": "5b71c93740ed29812ef583e270b14bcfa799b5634bd76c1f98fa31150de42ed4"
16631663
},
16641664
"src/openai/types/beta/agent.py": {
16651665
"mode": "100644",
@@ -1775,12 +1775,16 @@
17751775
},
17761776
"src/openai/types/beta/agent_session.py": {
17771777
"mode": "100644",
1778-
"sha256": "d7112cb67c59fbb1c993d666ea4ca8bea709de9c4c3b2738cd16d395744bc072"
1778+
"sha256": "524bd4ed6bc83747a6199379e82911b46b7aba6bd0668e339aeb12c013054130"
17791779
},
17801780
"src/openai/types/beta/agent_session_assistant_message.py": {
17811781
"mode": "100644",
17821782
"sha256": "cd658ea7c56193403b7188950c00702190c929a72fc3d0be0e7853d710c4f57a"
17831783
},
1784+
"src/openai/types/beta/agent_session_budget_reached_event.py": {
1785+
"mode": "100644",
1786+
"sha256": "d4a4be2e3366b97aa8d9256c53b2278a6cbebafb0004fc5f49951bc7d05d437a"
1787+
},
17841788
"src/openai/types/beta/agent_session_created_event.py": {
17851789
"mode": "100644",
17861790
"sha256": "ece7da18f956751a79a28098a8297f87c0adc9db4b8cf36b7528941a08fa5b1e"
@@ -1831,7 +1835,7 @@
18311835
},
18321836
"src/openai/types/beta/agent_session_event.py": {
18331837
"mode": "100644",
1834-
"sha256": "96b07cffe33f0ec960eeb92b317018f05c5f0a633d0f4b01e741073c085fdb5e"
1838+
"sha256": "2b42487182e4ba75a2a865f66f646c8f02cfad3597ec4ab897917d6f8fc569f6"
18351839
},
18361840
"src/openai/types/beta/agent_session_failed_event.py": {
18371841
"mode": "100644",
@@ -2091,15 +2095,15 @@
20912095
},
20922096
"src/openai/types/beta/agents/session_create_params.py": {
20932097
"mode": "100644",
2094-
"sha256": "32b7560fe3a117d1cd50ea68affa615b4c35dd17cb845dc2ec4f5dc1cd3b7682"
2098+
"sha256": "44261a235f763e718d5c71de57cc87eb7c2e502ada26c350c62e7b1d2c73bdf6"
20952099
},
20962100
"src/openai/types/beta/agents/session_list_params.py": {
20972101
"mode": "100644",
20982102
"sha256": "2de32d51ce2cfdbcf2224acfa8b04a28234717d6c373a3d68ee66bac90f75d2d"
20992103
},
21002104
"src/openai/types/beta/agents/session_update_params.py": {
21012105
"mode": "100644",
2102-
"sha256": "a3d82223cbaeff336175539efebf8998d6bc5193bcabcc82c1f11a724176f05d"
2106+
"sha256": "22f0f47d3b8158bb9aa9a588bec6944171432cddbb757aaeaf8c0602a2b232a6"
21032107
},
21042108
"src/openai/types/beta/agents/sessions/__init__.py": {
21052109
"mode": "100644",
@@ -3531,7 +3535,7 @@
35313535
},
35323536
"src/openai/types/beta/session_turn_error.py": {
35333537
"mode": "100644",
3534-
"sha256": "5c001662a8a2ba24efe4d7d42a4c37ba70e9c45e095ebfd3c973fa32af48ad7e"
3538+
"sha256": "0509a933cb578c18a49dd1abf2d689bf4da1c6e8bb8b7e80d5d024c22b41042e"
35353539
},
35363540
"src/openai/types/beta/setup_command_param.py": {
35373541
"mode": "100644",
@@ -7811,7 +7815,7 @@
78117815
},
78127816
"tests/api_resources/beta/agents/test_sessions.py": {
78137817
"mode": "100644",
7814-
"sha256": "1bda32e69765f37c574631637155008a86e4e66973581112e8a4aaa37ba21e44"
7818+
"sha256": "8f3c9a080fa915a1febed855312d597184431105e4227c1f4acfd24a42dd8ee9"
78157819
},
78167820
"tests/api_resources/beta/agents/test_vaults.py": {
78177821
"mode": "100644",

0 commit comments

Comments
 (0)