Skip to content

Commit 8618c60

Browse files
committed
code review : rename IndividualRequestUsage to RequestUsage
1 parent 873015b commit 8618c60

File tree

2 files changed

+58
-58
lines changed

2 files changed

+58
-58
lines changed

src/agents/usage.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
@dataclass
8-
class IndividualRequestUsage:
8+
class RequestUsage:
99
"""Usage details for a single API request.
1010
1111
This is useful for cost calculation when different pricing rates apply based on
@@ -51,7 +51,7 @@ class Usage:
5151
total_tokens: int = 0
5252
"""Total tokens sent and received, across all requests."""
5353

54-
individual_requests: list[IndividualRequestUsage] = field(default_factory=list)
54+
request_usage_entries: list[RequestUsage] = field(default_factory=list)
5555
"""List of individual request usage details for accurate per-request cost calculation.
5656
5757
This field preserves the token counts for each individual API request made during a run.
@@ -64,7 +64,7 @@ class Usage:
6464
6565
Example:
6666
For a run that makes 3 API calls with 100K, 150K, and 80K input tokens each,
67-
the aggregated `input_tokens` would be 330K, but `individual_requests` would
67+
the aggregated `input_tokens` would be 330K, but `request_usage_entries` would
6868
preserve the [100K, 150K, 80K] breakdown needed for accurate cost calculation.
6969
"""
7070

@@ -73,7 +73,7 @@ def add(self, other: "Usage") -> None:
7373
7474
This method automatically preserves individual request details for accurate
7575
cost calculation. When adding a Usage object that represents a single request
76-
(requests=1), it creates an IndividualRequestUsage entry to preserve the
76+
(requests=1), it creates an RequestUsage entry to preserve the
7777
per-request token breakdown.
7878
7979
Args:
@@ -96,14 +96,14 @@ def add(self, other: "Usage") -> None:
9696
# Automatically preserve individual request details for accurate cost calculation.
9797
# If the other Usage represents a single request with tokens, record it.
9898
if other.requests == 1 and other.total_tokens > 0:
99-
individual_usage = IndividualRequestUsage(
99+
individual_usage = RequestUsage(
100100
input_tokens=other.input_tokens,
101101
output_tokens=other.output_tokens,
102102
total_tokens=other.total_tokens,
103103
input_tokens_details=other.input_tokens_details,
104104
output_tokens_details=other.output_tokens_details,
105105
)
106-
self.individual_requests.append(individual_usage)
107-
elif other.individual_requests:
106+
self.request_usage_entries.append(individual_usage)
107+
elif other.request_usage_entries:
108108
# If the other Usage already has individual request breakdowns, merge them.
109-
self.individual_requests.extend(other.individual_requests)
109+
self.request_usage_entries.extend(other.request_usage_entries)

tests/test_usage.py

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from openai.types.responses.response_usage import InputTokensDetails, OutputTokensDetails
22

3-
from agents.usage import IndividualRequestUsage, Usage
3+
from agents.usage import RequestUsage, Usage
44

55

66
def test_usage_add_aggregates_all_fields():
@@ -52,25 +52,25 @@ def test_usage_add_aggregates_with_none_values():
5252
assert u1.output_tokens_details.reasoning_tokens == 6
5353

5454

55-
def test_individual_request_usage_creation():
56-
"""Test that IndividualRequestUsage is created correctly."""
57-
individual = IndividualRequestUsage(
55+
def test_request_usage_creation():
56+
"""Test that RequestUsage is created correctly."""
57+
request_usage = RequestUsage(
5858
input_tokens=100,
5959
output_tokens=200,
6060
total_tokens=300,
6161
input_tokens_details=InputTokensDetails(cached_tokens=10),
6262
output_tokens_details=OutputTokensDetails(reasoning_tokens=20),
6363
)
6464

65-
assert individual.input_tokens == 100
66-
assert individual.output_tokens == 200
67-
assert individual.total_tokens == 300
68-
assert individual.input_tokens_details.cached_tokens == 10
69-
assert individual.output_tokens_details.reasoning_tokens == 20
65+
assert request_usage.input_tokens == 100
66+
assert request_usage.output_tokens == 200
67+
assert request_usage.total_tokens == 300
68+
assert request_usage.input_tokens_details.cached_tokens == 10
69+
assert request_usage.output_tokens_details.reasoning_tokens == 20
7070

7171

7272
def test_usage_add_preserves_single_request():
73-
"""Test that adding a single request Usage creates an IndividualRequestUsage entry."""
73+
"""Test that adding a single request Usage creates an RequestUsage entry."""
7474
u1 = Usage()
7575
u2 = Usage(
7676
requests=1,
@@ -83,18 +83,18 @@ def test_usage_add_preserves_single_request():
8383

8484
u1.add(u2)
8585

86-
# Should preserve the individual request details
87-
assert len(u1.individual_requests) == 1
88-
individual = u1.individual_requests[0]
89-
assert individual.input_tokens == 100
90-
assert individual.output_tokens == 200
91-
assert individual.total_tokens == 300
92-
assert individual.input_tokens_details.cached_tokens == 10
93-
assert individual.output_tokens_details.reasoning_tokens == 20
86+
# Should preserve the request usage details
87+
assert len(u1.request_usage_entries) == 1
88+
request_usage = u1.request_usage_entries[0]
89+
assert request_usage.input_tokens == 100
90+
assert request_usage.output_tokens == 200
91+
assert request_usage.total_tokens == 300
92+
assert request_usage.input_tokens_details.cached_tokens == 10
93+
assert request_usage.output_tokens_details.reasoning_tokens == 20
9494

9595

9696
def test_usage_add_ignores_zero_token_requests():
97-
"""Test that zero-token requests don't create IndividualRequestUsage entries."""
97+
"""Test that zero-token requests don't create request_usage_entries."""
9898
u1 = Usage()
9999
u2 = Usage(
100100
requests=1,
@@ -107,12 +107,12 @@ def test_usage_add_ignores_zero_token_requests():
107107

108108
u1.add(u2)
109109

110-
# Should not create an individual request entry for zero tokens
111-
assert len(u1.individual_requests) == 0
110+
# Should not create a request_usage_entry for zero tokens
111+
assert len(u1.request_usage_entries) == 0
112112

113113

114114
def test_usage_add_ignores_multi_request_usage():
115-
"""Test that multi-request Usage objects don't create IndividualRequestUsage entries."""
115+
"""Test that multi-request Usage objects don't create request_usage_entries."""
116116
u1 = Usage()
117117
u2 = Usage(
118118
requests=3, # Multiple requests
@@ -125,13 +125,13 @@ def test_usage_add_ignores_multi_request_usage():
125125

126126
u1.add(u2)
127127

128-
# Should not create an individual request entry for multi-request usage
129-
assert len(u1.individual_requests) == 0
128+
# Should not create a request usage entry for multi-request usage
129+
assert len(u1.request_usage_entries) == 0
130130

131131

132-
def test_usage_add_merges_existing_individual_requests():
133-
"""Test that existing individual_requests are merged when adding Usage objects."""
134-
# Create first usage with individual requests
132+
def test_usage_add_merges_existing_request_usage_entries():
133+
"""Test that existing request_usage_entries are merged when adding Usage objects."""
134+
# Create first usage with request_usage_entries
135135
u1 = Usage()
136136
u2 = Usage(
137137
requests=1,
@@ -143,7 +143,7 @@ def test_usage_add_merges_existing_individual_requests():
143143
)
144144
u1.add(u2)
145145

146-
# Create second usage with individual requests
146+
# Create second usage with request_usage_entries
147147
u3 = Usage(
148148
requests=1,
149149
input_tokens=50,
@@ -155,27 +155,27 @@ def test_usage_add_merges_existing_individual_requests():
155155

156156
u1.add(u3)
157157

158-
# Should have both individual requests
159-
assert len(u1.individual_requests) == 2
158+
# Should have both request_usage_entries
159+
assert len(u1.request_usage_entries) == 2
160160

161161
# First request
162-
first = u1.individual_requests[0]
162+
first = u1.request_usage_entries[0]
163163
assert first.input_tokens == 100
164164
assert first.output_tokens == 200
165165
assert first.total_tokens == 300
166166

167167
# Second request
168-
second = u1.individual_requests[1]
168+
second = u1.request_usage_entries[1]
169169
assert second.input_tokens == 50
170170
assert second.output_tokens == 75
171171
assert second.total_tokens == 125
172172

173173

174-
def test_usage_add_with_pre_existing_individual_requests():
175-
"""Test adding Usage objects that already have individual_requests."""
174+
def test_usage_add_with_pre_existing_request_usage_entries():
175+
"""Test adding Usage objects that already have request_usage_entries."""
176176
u1 = Usage()
177177

178-
# Create a usage with individual requests
178+
# Create a usage with request_usage_entries
179179
u2 = Usage(
180180
requests=1,
181181
input_tokens=100,
@@ -186,7 +186,7 @@ def test_usage_add_with_pre_existing_individual_requests():
186186
)
187187
u1.add(u2)
188188

189-
# Create another usage with individual requests
189+
# Create another usage with request_usage_entries
190190
u3 = Usage(
191191
requests=1,
192192
input_tokens=50,
@@ -199,16 +199,16 @@ def test_usage_add_with_pre_existing_individual_requests():
199199
# Add u3 to u1
200200
u1.add(u3)
201201

202-
# Should have both individual requests
203-
assert len(u1.individual_requests) == 2
204-
assert u1.individual_requests[0].input_tokens == 100
205-
assert u1.individual_requests[1].input_tokens == 50
202+
# Should have both request_usage_entries
203+
assert len(u1.request_usage_entries) == 2
204+
assert u1.request_usage_entries[0].input_tokens == 100
205+
assert u1.request_usage_entries[1].input_tokens == 50
206206

207207

208-
def test_usage_individual_requests_default_empty():
209-
"""Test that individual_requests defaults to an empty list."""
208+
def test_usage_request_usage_entries_default_empty():
209+
"""Test that request_usage_entries defaults to an empty list."""
210210
u = Usage()
211-
assert u.individual_requests == []
211+
assert u.request_usage_entries == []
212212

213213

214214
def test_anthropic_cost_calculation_scenario():
@@ -257,13 +257,13 @@ def test_anthropic_cost_calculation_scenario():
257257
assert usage.output_tokens == 165_000 # 50K + 75K + 40K
258258
assert usage.total_tokens == 495_000 # 150K + 225K + 120K
259259

260-
# Verify individual request preservation
261-
assert len(usage.individual_requests) == 3
262-
assert usage.individual_requests[0].input_tokens == 100_000
263-
assert usage.individual_requests[1].input_tokens == 150_000
264-
assert usage.individual_requests[2].input_tokens == 80_000
260+
# Verify request_usage_entries preservation
261+
assert len(usage.request_usage_entries) == 3
262+
assert usage.request_usage_entries[0].input_tokens == 100_000
263+
assert usage.request_usage_entries[1].input_tokens == 150_000
264+
assert usage.request_usage_entries[2].input_tokens == 80_000
265265

266-
# All individual requests are under 200K threshold
267-
for req in usage.individual_requests:
266+
# All request_usage_entries are under 200K threshold
267+
for req in usage.request_usage_entries:
268268
assert req.input_tokens < 200_000
269269
assert req.output_tokens < 200_000

0 commit comments

Comments
 (0)