|
| 1 | +"""Tests that the openai_agents temporal models copy real token usage onto spans. |
| 2 | +
|
| 3 | +The backend bills per-call usage from ``span.output["usage"]``; these tests |
| 4 | +assert each model writes the framework-reported usage there (and, for the |
| 5 | +streaming model, into the returned ``ModelResponse.usage``) instead of |
| 6 | +dropping it. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from datetime import UTC, datetime |
| 12 | +from contextlib import asynccontextmanager |
| 13 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 14 | + |
| 15 | +import pytest |
| 16 | +from agents import ModelResponse |
| 17 | +from agents.usage import Usage, InputTokensDetails, OutputTokensDetails |
| 18 | + |
| 19 | +from agentex.types.span import Span |
| 20 | + |
| 21 | +pytestmark = pytest.mark.asyncio |
| 22 | + |
| 23 | + |
| 24 | +class FakeTrace: |
| 25 | + """Captures spans handed out by trace.span() so tests can inspect them.""" |
| 26 | + |
| 27 | + def __init__(self) -> None: |
| 28 | + self.spans: list[Span] = [] |
| 29 | + |
| 30 | + @asynccontextmanager |
| 31 | + async def span(self, name, parent_id=None, input=None, data=None, task_id=None): |
| 32 | + span = Span( |
| 33 | + id=f"span-{len(self.spans)}", |
| 34 | + name=name, |
| 35 | + start_time=datetime.now(UTC), |
| 36 | + trace_id="trace-1", |
| 37 | + parent_id=parent_id, |
| 38 | + input=input, |
| 39 | + data=data, |
| 40 | + task_id=task_id, |
| 41 | + ) |
| 42 | + self.spans.append(span) |
| 43 | + yield span |
| 44 | + |
| 45 | + |
| 46 | +class FakeTracer: |
| 47 | + def __init__(self) -> None: |
| 48 | + self.trace_obj = FakeTrace() |
| 49 | + |
| 50 | + def trace(self, trace_id): |
| 51 | + return self.trace_obj |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def tracing_contextvars(): |
| 56 | + from agentex.lib.core.temporal.plugins.openai_agents.interceptors.context_interceptor import ( |
| 57 | + streaming_task_id, |
| 58 | + streaming_trace_id, |
| 59 | + streaming_parent_span_id, |
| 60 | + ) |
| 61 | + |
| 62 | + tokens = [ |
| 63 | + streaming_task_id.set("task-1"), |
| 64 | + streaming_trace_id.set("trace-1"), |
| 65 | + streaming_parent_span_id.set("parent-span-1"), |
| 66 | + ] |
| 67 | + yield |
| 68 | + streaming_task_id.reset(tokens[0]) |
| 69 | + streaming_trace_id.reset(tokens[1]) |
| 70 | + streaming_parent_span_id.reset(tokens[2]) |
| 71 | + |
| 72 | + |
| 73 | +def _agents_usage() -> Usage: |
| 74 | + return Usage( |
| 75 | + requests=1, |
| 76 | + input_tokens=120, |
| 77 | + output_tokens=80, |
| 78 | + total_tokens=200, |
| 79 | + input_tokens_details=InputTokensDetails(cached_tokens=30), |
| 80 | + output_tokens_details=OutputTokensDetails(reasoning_tokens=40), |
| 81 | + ) |
| 82 | + |
| 83 | +EXPECTED_USAGE_BLOB = { |
| 84 | + "input_tokens": 120, |
| 85 | + "output_tokens": 80, |
| 86 | + "total_tokens": 200, |
| 87 | + "cached_input_tokens": 30, |
| 88 | + "reasoning_tokens": 40, |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +class TestTemporalTracingModels: |
| 93 | + async def _run_wrapper(self, wrapper_cls) -> Span: |
| 94 | + tracer = FakeTracer() |
| 95 | + base_model = MagicMock() |
| 96 | + base_model.model = "gpt-4o" |
| 97 | + base_model.get_response = AsyncMock( |
| 98 | + return_value=ModelResponse(output=[], usage=_agents_usage(), response_id="resp-1") |
| 99 | + ) |
| 100 | + |
| 101 | + model = wrapper_cls(base_model, tracer) |
| 102 | + from agents import ModelSettings |
| 103 | + |
| 104 | + response = await model.get_response( |
| 105 | + system_instructions=None, |
| 106 | + input="hello", |
| 107 | + model_settings=ModelSettings(), |
| 108 | + tools=[], |
| 109 | + output_schema=None, |
| 110 | + handoffs=[], |
| 111 | + tracing=None, |
| 112 | + ) |
| 113 | + assert response.usage.input_tokens == 120 |
| 114 | + assert len(tracer.trace_obj.spans) == 1 |
| 115 | + return tracer.trace_obj.spans[0] |
| 116 | + |
| 117 | + async def test_responses_model_writes_usage_to_span_output(self, tracing_contextvars): |
| 118 | + from agentex.lib.core.temporal.plugins.openai_agents.models.temporal_tracing_model import ( |
| 119 | + TemporalTracingResponsesModel, |
| 120 | + ) |
| 121 | + |
| 122 | + span = await self._run_wrapper(TemporalTracingResponsesModel) |
| 123 | + assert span.output["usage"] == EXPECTED_USAGE_BLOB |
| 124 | + |
| 125 | + async def test_chat_completions_model_writes_usage_to_span_output(self, tracing_contextvars): |
| 126 | + from agentex.lib.core.temporal.plugins.openai_agents.models.temporal_tracing_model import ( |
| 127 | + TemporalTracingChatCompletionsModel, |
| 128 | + ) |
| 129 | + |
| 130 | + span = await self._run_wrapper(TemporalTracingChatCompletionsModel) |
| 131 | + assert span.output["usage"] == EXPECTED_USAGE_BLOB |
| 132 | + |
| 133 | + |
| 134 | +class FakeStream: |
| 135 | + def __init__(self, events) -> None: |
| 136 | + self._events = events |
| 137 | + |
| 138 | + def __aiter__(self): |
| 139 | + async def gen(): |
| 140 | + for event in self._events: |
| 141 | + yield event |
| 142 | + |
| 143 | + return gen() |
| 144 | + |
| 145 | + |
| 146 | +class TestTemporalStreamingModel: |
| 147 | + async def test_streaming_model_captures_final_response_usage(self, tracing_contextvars): |
| 148 | + import agentex.lib.core.temporal.plugins.openai_agents.models.temporal_streaming_model as tsm |
| 149 | + |
| 150 | + from openai.types.responses import Response, ResponseCompletedEvent |
| 151 | + from openai.types.responses.response_usage import ( |
| 152 | + ResponseUsage, |
| 153 | + InputTokensDetails as ResponseInputTokensDetails, |
| 154 | + OutputTokensDetails as ResponseOutputTokensDetails, |
| 155 | + ) |
| 156 | + |
| 157 | + usage = ResponseUsage( |
| 158 | + input_tokens=120, |
| 159 | + output_tokens=80, |
| 160 | + total_tokens=200, |
| 161 | + input_tokens_details=ResponseInputTokensDetails(cached_tokens=30), |
| 162 | + output_tokens_details=ResponseOutputTokensDetails(reasoning_tokens=40), |
| 163 | + ) |
| 164 | + completed = ResponseCompletedEvent.model_construct( |
| 165 | + type="response.completed", |
| 166 | + response=Response.model_construct(output=[], usage=usage), |
| 167 | + ) |
| 168 | + |
| 169 | + fake_tracer = FakeTracer() |
| 170 | + openai_client = MagicMock() |
| 171 | + openai_client.responses.create = AsyncMock(return_value=FakeStream([completed])) |
| 172 | + |
| 173 | + with ( |
| 174 | + patch.object(tsm, "create_async_agentex_client", return_value=MagicMock()), |
| 175 | + patch.object(tsm, "AsyncTracer", return_value=fake_tracer), |
| 176 | + ): |
| 177 | + model = tsm.TemporalStreamingModel(model_name="gpt-4o", openai_client=openai_client) |
| 178 | + |
| 179 | + from agents import ModelSettings |
| 180 | + |
| 181 | + response = await model.get_response( |
| 182 | + system_instructions=None, |
| 183 | + input="hello", |
| 184 | + model_settings=ModelSettings(), |
| 185 | + tools=[], |
| 186 | + output_schema=None, |
| 187 | + handoffs=[], |
| 188 | + tracing=None, |
| 189 | + ) |
| 190 | + |
| 191 | + # Real usage lands on the returned ModelResponse (was zeroed before) |
| 192 | + assert response.usage.requests == 1 |
| 193 | + assert response.usage.input_tokens == 120 |
| 194 | + assert response.usage.output_tokens == 80 |
| 195 | + assert response.usage.total_tokens == 200 |
| 196 | + assert response.usage.input_tokens_details.cached_tokens == 30 |
| 197 | + assert response.usage.output_tokens_details.reasoning_tokens == 40 |
| 198 | + |
| 199 | + # And on the span output for billing |
| 200 | + assert len(fake_tracer.trace_obj.spans) == 1 |
| 201 | + span = fake_tracer.trace_obj.spans[0] |
| 202 | + assert span.output["usage"] == EXPECTED_USAGE_BLOB |
| 203 | + |
| 204 | + async def test_streaming_model_omits_usage_when_api_reports_none(self, tracing_contextvars): |
| 205 | + import agentex.lib.core.temporal.plugins.openai_agents.models.temporal_streaming_model as tsm |
| 206 | + |
| 207 | + from openai.types.responses import Response, ResponseCompletedEvent |
| 208 | + |
| 209 | + completed = ResponseCompletedEvent.model_construct( |
| 210 | + type="response.completed", |
| 211 | + response=Response.model_construct(output=[], usage=None), |
| 212 | + ) |
| 213 | + |
| 214 | + fake_tracer = FakeTracer() |
| 215 | + openai_client = MagicMock() |
| 216 | + openai_client.responses.create = AsyncMock(return_value=FakeStream([completed])) |
| 217 | + |
| 218 | + with ( |
| 219 | + patch.object(tsm, "create_async_agentex_client", return_value=MagicMock()), |
| 220 | + patch.object(tsm, "AsyncTracer", return_value=fake_tracer), |
| 221 | + ): |
| 222 | + model = tsm.TemporalStreamingModel(model_name="gpt-4o", openai_client=openai_client) |
| 223 | + |
| 224 | + from agents import ModelSettings |
| 225 | + |
| 226 | + response = await model.get_response( |
| 227 | + system_instructions=None, |
| 228 | + input="hello", |
| 229 | + model_settings=ModelSettings(), |
| 230 | + tools=[], |
| 231 | + output_schema=None, |
| 232 | + handoffs=[], |
| 233 | + tracing=None, |
| 234 | + ) |
| 235 | + |
| 236 | + assert response.usage.input_tokens == 0 |
| 237 | + span = fake_tracer.trace_obj.spans[0] |
| 238 | + assert "usage" not in span.output |
0 commit comments