Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .castiron.stats.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
schema_version: 1
generation_id: 246f879d-8464-4b41-bb24-d89bc1e8df3c
openapi_spec_hash: 0ce316f78a89857b9483e1ec86e095ea
openapi_transformed_spec_hash: 45b9b080467ec914b61d840811bed260
generation_id: 4ac07400-aa52-4afc-a188-16bded98a14e
openapi_spec_hash: eb7c75c645ed03c9a5db741cde8046d8
openapi_transformed_spec_hash: 9a0e7dbd3f5ab2463dfc8f22c819b565
config_hash: 036100415594abcf279907b22cc8d2f1
codegen_sha: cc041f0d27315d1a33d649dd1e5fb6f2ec04c72b
codegen_hash: 52084edd6ec47c7ad6066c4f16600f52381ceb96fcad79a85380928a81299e80
codegen_sha: c9edf71608b6c812f706a588826aef75ff997276
codegen_hash: 9443f963a42f341bc902727ed8ce8358ac6d9c7e6da33c5e97ccfaa0cff2bac9
11 changes: 8 additions & 3 deletions api_reference/openapi.transformed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92123,6 +92123,11 @@ components:
- 'null'
format: int64
description: The Unix timestamp, in seconds, when the subagent was closed. Null while active, including after resume.
usage:
anyOf:
- $ref: '#/components/schemas/TokenUsageResource'
- type: 'null'
description: Best-effort token usage across this subagent's own turns, including turns after a resume. On list and retrieve, null means a subtotal is unavailable. Nested subagents have their own subtotals. Session usage already includes these tokens.
required:
- id
- object
Expand Down Expand Up @@ -93603,7 +93608,7 @@ components:
anyOf:
- $ref: '#/components/schemas/TokenUsageResource'
- type: 'null'
description: Best-effort token usage for the turn, or null if unknown. Recorded usage may change.
description: Best-effort token usage for the turn, or null if unknown. A root turn includes usage from subagent turns attributed to it. A subagent turn reports its own usage. Recorded usage may change.
required:
- id
- object
Expand Down Expand Up @@ -95449,7 +95454,7 @@ components:
- integer
- 'null'
minimum: 0
description: Best-effort recorded spend floored to whole USD cents, or null when unavailable.
description: Best-effort recorded spend for the session, including its subagents, floored to whole USD cents. Null when unavailable.
required:
- limit
- consumed
Expand Down Expand Up @@ -95574,7 +95579,7 @@ components:
anyOf:
- $ref: '#/components/schemas/TokenUsageResource'
- type: 'null'
description: Best-effort token usage for the session, or null if unknown. Recorded usage may change.
description: Best-effort token usage for the session, including its subagents, or null if unknown. Recorded usage may change.
required:
- spend_control
- metadata
Expand Down
6 changes: 3 additions & 3 deletions scripts/castiron/generated-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@
},
"src/openai/types/beta/agent_session.py": {
"mode": "100644",
"sha256": "03ba34e1d398eff589652979a9454d95a9a43478e6a44a35a2adb33d02cacb09"
"sha256": "aad058844c2b5344fa81675d877a4b0425161a19c572b5d7a728f7652298a594"
},
"src/openai/types/beta/agent_session_assistant_message.py": {
"mode": "100644",
Expand Down Expand Up @@ -2363,7 +2363,7 @@
},
"src/openai/types/beta/agents/sessions/turn.py": {
"mode": "100644",
"sha256": "761ca9a889e858764e75aa56d624a329a9cc5bf6c4ff877bc5d232d827460357"
"sha256": "bc3d28085e2923e509f6d18685c7449e524fa97092a5fa005f7a22f88ee3c323"
},
"src/openai/types/beta/agents/sessions/turn_list_params.py": {
"mode": "100644",
Expand Down Expand Up @@ -3739,7 +3739,7 @@
},
"src/openai/types/beta/subagent.py": {
"mode": "100644",
"sha256": "ff5ff857b79d55cc03953cca15a3569bff17bf36be1353abc0ae8f569d14b4b6"
"sha256": "429c282486fa6d5a1599e81b7287bbab119485e1b304bb40d140eae645292abc"
},
"src/openai/types/beta/summary_text.py": {
"mode": "100644",
Expand Down
9 changes: 5 additions & 4 deletions src/openai/types/beta/agent_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ class SpendControl(BaseModel):

consumed: Optional[int] = None
"""
Best-effort recorded spend floored to whole USD cents, or null when unavailable.
Best-effort recorded spend for the session, including its subagents, floored to
whole USD cents. Null when unavailable.
"""

limit: Optional[int] = None
Expand Down Expand Up @@ -331,9 +332,9 @@ class AgentSession(BaseModel):
"""

usage: Optional[TokenUsage] = None
"""Best-effort token usage for the session, or null if unknown.

Recorded usage may change.
"""
Best-effort token usage for the session, including its subagents, or null if
unknown. Recorded usage may change.
"""

vault_ids: List[str]
Expand Down
3 changes: 2 additions & 1 deletion src/openai/types/beta/agents/sessions/turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class Turn(BaseModel):
usage: Optional[TokenUsage] = None
"""Best-effort token usage for the turn, or null if unknown.

Recorded usage may change.
A root turn includes usage from subagent turns attributed to it. A subagent turn
reports its own usage. Recorded usage may change.
"""

prompt_cache_options: Optional[PromptCacheOptions] = None
Expand Down
8 changes: 8 additions & 0 deletions src/openai/types/beta/subagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing_extensions import Literal

from ..._models import BaseModel
from .token_usage import TokenUsage
from .agent_content import AgentContent

__all__ = ["Subagent"]
Expand Down Expand Up @@ -52,3 +53,10 @@ class Subagent(BaseModel):
- `active` - The subagent remains available, including while idle between turns.
- `closed` - The subagent is closed.
"""

usage: Optional[TokenUsage] = None
"""
Best-effort token usage across this subagent's own turns, including turns after
a resume. On list and retrieve, null means a subtotal is unavailable. Nested
subagents have their own subtotals. Session usage already includes these tokens.
"""
57 changes: 57 additions & 0 deletions tests/test_subagent_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import httpx2 as httpx

from openai import OpenAI


def test_subagent_usage_on_list_and_retrieve() -> None:
subagent = {
"id": "sub_test",
"object": "agent.session.subagent",
"session_id": "session_test",
"name": None,
"instructions": None,
"parent_agent_id": "parent_test",
"status": "active",
"opened_at": 42,
"closed_at": None,
}
counts = {
"input_tokens": 97,
"input_tokens_details": {"cached_tokens": 13},
"output_tokens": 31,
"output_tokens_details": {"reasoning_tokens": 7},
"total_tokens": 128,
}
populated = {**subagent, "usage": counts}
explicit_null = {**subagent, "usage": None}
records = [populated, explicit_null, subagent]
page = {"object": "list", "data": records, "first_id": "sub_test", "last_id": "sub_test", "has_more": False}
path = "/agents/sessions/session_test/subagents"
replies = iter([(f"{path}/sub_test", record) for record in records] + [(path, page)])

def handle(request: httpx.Request) -> httpx.Response:
expected_path, body = next(replies)
assert request.method == "GET" and request.url.path == expected_path
assert request.headers["OpenAI-Beta"] == "agents=v1"
return httpx.Response(200, json=body)

with OpenAI(
base_url="https://example.invalid",
api_key="synthetic-key",
http_client=httpx.Client(transport=httpx.MockTransport(handle)),
max_retries=0,
_strict_response_validation=True,
) as client:
subagents = client.beta.agents.sessions.subagents
retrieved = [subagents.retrieve("sub_test", session_id="session_test") for _ in records]
listed = subagents.list("session_test").data
for rows in (retrieved, listed):
usage = rows[0].usage
assert usage is not None and usage.model_dump() == counts
assert rows[1].usage is None and "usage" in rows[1].model_fields_set
assert rows[2].usage is None and "usage" not in rows[2].model_fields_set
assert next(replies, None) is None


if __name__ == "__main__":
test_subagent_usage_on_list_and_retrieve()
Loading