Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix(agent): preserve complete model usage pricing (#39201)
zyssyz12311016563b1c6
diff --git a/api/tests/unit_tests/clients/agent_backend/test_event_adapter.py b/api/tests/unit_tests/clients/agent_backend/test_event_adapter.pyindex 4adef713cfd..14c51895df0 100644--- a/api/tests/unit_tests/clients/agent_backend/test_event_adapter.py+++ b/api/tests/unit_tests/clients/agent_backend/test_event_adapter.py@@ -1,3 +1,5 @@+from decimal import Decimal+import pytestfrom agenton.compositor import CompositorSessionSnapshotfrom dify_agent.protocol import (@@ -83,7 +85,19 @@ def test_event_adapter_maps_run_succeeded_to_final_output():data=RunSucceededEventData(output={"summary": "done"},session_snapshot=snapshot,- usage=AgentRunUsage(prompt_tokens=2, completion_tokens=3),+ usage=AgentRunUsage(+ prompt_tokens=2,+ prompt_unit_price=Decimal(5),+ prompt_price_unit=Decimal("0.000001"),+ prompt_price=Decimal("0.000010"),+ completion_tokens=3,+ completion_unit_price=Decimal(30),+ completion_price_unit=Decimal("0.000001"),+ completion_price=Decimal("0.000090"),+ total_price=Decimal("0.000100"),+ currency="USD",+ latency=0.4,+ ),),))@@ -94,7 +108,22 @@ def test_event_adapter_maps_run_succeeded_to_final_output():source_event_id="3-0",output={"summary": "done"},session_snapshot=snapshot,- usage={"prompt_tokens": 2, "completion_tokens": 3, "total_tokens": 5},+ usage={+ "prompt_tokens": 2,+ "prompt_unit_price": "5",+ "prompt_price_unit": "0.000001",+ "prompt_price": "0.000010",+ "completion_tokens": 3,+ "completion_unit_price": "30",+ "completion_price_unit": "0.000001",+ "completion_price": "0.000090",+ "total_tokens": 5,+ "total_price": "0.000100",+ "currency": "USD",+ "latency": 0.4,+ "time_to_first_token": None,+ "time_to_generate": None,+ },)]diff --git a/api/tests/unit_tests/core/app/apps/agent_app/test_app_runner.py b/api/tests/unit_tests/core/app/apps/agent_app/test_app_runner.pyindex 6321c8ea48b..f623dd767d2 100644--- a/api/tests/unit_tests/core/app/apps/agent_app/test_app_runner.py+++ b/api/tests/unit_tests/core/app/apps/agent_app/test_app_runner.py@@ -6,6 +6,7 @@ from __future__ import annotationsfrom collections.abc import Callable, Iteratorfrom datetime import UTC, datetime+from decimal import Decimalfrom types import SimpleNamespacefrom typing import Any, overridefrom unittest.mock import MagicMock@@ -137,7 +138,19 @@ class _StreamingFakeAgentBackendRunClient(FakeAgentBackendRunClient):data=RunSucceededEventData(output={"text": "hello agent"},session_snapshot=CompositorSessionSnapshot(layers=[]),- usage=AgentRunUsage(prompt_tokens=3, completion_tokens=5),+ usage=AgentRunUsage(+ prompt_tokens=3,+ prompt_unit_price=Decimal(5),+ prompt_price_unit=Decimal("0.000001"),+ prompt_price=Decimal("0.000015"),+ completion_tokens=5,+ completion_unit_price=Decimal(30),+ completion_price_unit=Decimal("0.000001"),+ completion_price=Decimal("0.000150"),+ total_price=Decimal("0.000165"),+ currency="USD",+ latency=0.5,+ ),),)@@ -714,6 +727,10 @@ def test_successful_turn_routes_stream_text_to_agent_message_and_uses_terminal_oassert end_events[0].llm_result.usage.prompt_tokens == 3assert end_events[0].llm_result.usage.completion_tokens == 5assert end_events[0].llm_result.usage.total_tokens == 8+ assert end_events[0].llm_result.usage.prompt_price == Decimal("0.000015")+ assert end_events[0].llm_result.usage.completion_price == Decimal("0.000150")+ assert end_events[0].llm_result.usage.total_price == Decimal("0.000165")+ assert end_events[0].llm_result.usage.currency == "USD"rows = sorted(fake_session.rows.values(), key=lambda row: row.position)assert rows == []assert store.saveddiff --git a/api/tests/unit_tests/core/workflow/nodes/agent_v2/test_output_adapter.py b/api/tests/unit_tests/core/workflow/nodes/agent_v2/test_output_adapter.pyindex 8d50021f8fe..760786c5b75 100644--- a/api/tests/unit_tests/core/workflow/nodes/agent_v2/test_output_adapter.py+++ b/api/tests/unit_tests/core/workflow/nodes/agent_v2/test_output_adapter.py@@ -1,3 +1,4 @@+from decimal import Decimalfrom unittest.mock import patchimport pytest@@ -523,24 +524,36 @@ def test_success_output_adapter_maps_backend_usage_to_llm_usage_and_metadata():source_event_id="2-0",output={"summary": "ok"},session_snapshot=CompositorSessionSnapshot(layers=[]),+ usage={+ "prompt_tokens": 10,+ "prompt_unit_price": "5",+ "prompt_price_unit": "0.000001",+ "prompt_price": "0.000050",+ "completion_tokens": 5,+ "completion_unit_price": "30",+ "completion_price_unit": "0.000001",+ "completion_price": "0.000150",+ "total_tokens": 15,+ "total_price": "0.000200",+ "currency": "USD",+ "latency": 0.5,+ },),inputs={},process_data={},- metadata={- "agent_backend": {- "usage": {- "prompt_tokens": 10,- "completion_tokens": 5,- "total_tokens": 15,- }- }- },+ metadata={},)assert result.llm_usage.prompt_tokens == 10assert result.llm_usage.completion_tokens == 5assert result.llm_usage.total_tokens == 15+ assert result.llm_usage.prompt_price == Decimal("0.000050")+ assert result.llm_usage.completion_price == Decimal("0.000150")+ assert result.llm_usage.total_price == Decimal("0.000200")+ assert result.llm_usage.currency == "USD"assert result.metadata[WorkflowNodeExecutionMetadataKey.TOTAL_TOKENS] == 15+ assert result.metadata[WorkflowNodeExecutionMetadataKey.TOTAL_PRICE] == Decimal("0.000200")+ assert result.metadata[WorkflowNodeExecutionMetadataKey.CURRENCY] == "USD"def test_failure_output_adapter_maps_cancelled_to_failure_code():diff --git a/dify-agent/src/dify_agent/adapters/llm/model.py b/dify-agent/src/dify_agent/adapters/llm/model.pyindex 60256446e17..3ffa4efd4e3 100644--- a/dify-agent/src/dify_agent/adapters/llm/model.py+++ b/dify-agent/src/dify_agent/adapters/llm/model.py@@ -3,6 +3,9 @@The API and agent layers are clients of the plugin daemon, not direct hosts of provider SDKimplementations. This adapter therefore targets the plugin-daemon dispatch protocol and mapsPydantic AI messages into the daemon's Graphon-compatible request and stream response schema.+Pydantic AI keeps token counts only, so the adapter separately accumulates the daemon's complete+Graphon usage for the lifetime of one model instance. The Agent runner creates one adapter per run+and reads that accumulated usage after all model/tool rounds finish."""from __future__ import annotations@@ -86,7 +89,12 @@ class _DifyRequestInput:@dataclass(slots=True)class DifyLLMAdapterModel(Model[DifyPluginDaemonLLMClient]):- """Use a Dify plugin-daemon transport plus request-level model identity."""+ """Use a Dify plugin-daemon transport and retain complete usage for one Agent run.++ A model instance belongs to one runner invocation. Pydantic AI may call it repeatedly while+ resolving tools or retrying structured output; those sequential requests are accumulated so+ the terminal event reflects every billed model round.+ """model: strdaemon_provider: DifyPluginDaemonProvider@@ -95,6 +103,7 @@ class DifyLLMAdapterModel(Model[DifyPluginDaemonLLMClient]):credentials: dict[str, object] = field(default_factory=dict, repr=False)model_profile: InitVar[ModelProfileSpec | None] = Nonemodel_settings: InitVar[ModelSettings | None] = None+ _accumulated_usage: LLMUsage | None = field(default=None, init=False, repr=False)def __post_init__(self,@@ -122,6 +131,11 @@ class DifyLLMAdapterModel(Model[DifyPluginDaemonLLMClient]):def system(self) -> str:return self.daemon_provider.name+ @property+ def accumulated_usage(self) -> LLMUsage | None:+ """Return complete daemon usage accumulated across successful model requests."""+ return self._accumulated_usage+@overrideasync def request(self,@@ -149,7 +163,9 @@ class DifyLLMAdapterModel(Model[DifyPluginDaemonLLMClient]):)async for _event in response:pass- return response.get()+ model_response = response.get()+ self._record_usage(response.dify_usage)+ return model_response@asynccontextmanager@override@@ -164,7 +180,7 @@ class DifyLLMAdapterModel(Model[DifyPluginDaemonLLMClient]):prepared_settings, prepared_params = self.prepare_request(model_settings, model_request_parameters)request_input = self._build_request_input(messages, prepared_settings, prepared_params)- yield DifyStreamedResponse(+ response = DifyStreamedResponse(model_request_parameters=prepared_params,chunks=self.daemon_provider.client.iter_llm_result_chunks(provider=self.model_provider,@@ -179,6 +195,17 @@ class DifyLLMAdapterModel(Model[DifyPluginDaemonLLMClient]):response_model_name=self.model_name,provider_name_value=self.system,)+ yield response+ self._record_usage(response.dify_usage)++ def _record_usage(self, usage: LLMUsage | None) -> None:+ """Add one completed daemon request to this run's usage total."""+ if usage is None:+ return+ if self._accumulated_usage is None:+ self._accumulated_usage = usage.model_copy(deep=True)+ return+ self._accumulated_usage = self._accumulated_usage.plus(usage)def _build_request_input(self,@@ -197,17 +224,25 @@ class DifyLLMAdapterModel(Model[DifyPluginDaemonLLMClient]):@dataclassclass DifyStreamedResponse(StreamedResponse):+ """Map one daemon response while retaining its latest complete usage payload.++ Some providers may repeat cumulative usage on more than one stream chunk. Keeping the latest+ payload lets the owning model count each request exactly once instead of summing stream chunks.+ """+chunks: AsyncIterator[LLMResultChunk]response_model_name: strprovider_name_value: str_timestamp: datetime = field(default_factory=lambda: datetime.now(timezone.utc))_embedded_thinking_parser: "_EmbeddedThinkingParser" = field(default_factory=lambda: _EmbeddedThinkingParser())+ _dify_usage: LLMUsage | None = field(default=None, init=False, repr=False)@overrideasync def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:chunk_sequence = 0async for chunk in self.chunks:if chunk.delta.usage is not None:+ self._dify_usage = chunk.delta.usageself._usage: RequestUsage = _map_usage(chunk.delta.usage)if chunk.delta.finish_reason is not None:self.finish_reason: FinishReason | None = _normalize_finish_reason(chunk.delta.finish_reason)@@ -225,6 +260,11 @@ class DifyStreamedResponse(StreamedResponse):for event in self._embedded_thinking_parser.flush(self._parts_manager, self.provider_name_value):yield event+ @property+ def dify_usage(self) -> LLMUsage | None:+ """Return the daemon's complete usage for this model request."""+ return self._dify_usage+@property@overridedef model_name(self) -> str:diff --git a/dify-agent/src/dify_agent/protocol/schemas.py b/dify-agent/src/dify_agent/protocol/schemas.pyindex 1cfafdc8123..b4ea3843918 100644--- a/dify-agent/src/dify_agent/protocol/schemas.py+++ b/dify-agent/src/dify_agent/protocol/schemas.py@@ -39,6 +39,7 @@ composition and the runtime can rebuild the same structured output contract.from __future__ import annotationsfrom datetime import datetime, timezone+from decimal import Decimalfrom typing import Annotated, ClassVar, Final, Literal, TypeAliasfrom pydantic import BaseModel, ConfigDict, Field, JsonValue, TypeAdapter, model_serializer, model_validator@@ -254,11 +255,26 @@ class DeferredToolCallPayload(BaseModel):class AgentRunUsage(BaseModel):- """Token usage reported by the model request behind one Agent run."""+ """Complete model usage reported for one Agent run.++ Pricing fields default to zero so events from older Agent backend versions that contain only+ token counts remain valid when a new consumer replays persisted Redis streams.+ """prompt_tokens: int = 0+ prompt_unit_price: Decimal = Decimal(0)+ prompt_price_unit: Decimal = Decimal(0)+ prompt_price: Decimal = Decimal(0)completion_tokens: int = 0+ completion_unit_price: Decimal = Decimal(0)+ completion_price_unit: Decimal = Decimal(0)+ completion_price: Decimal = Decimal(0)total_tokens: int = 0+ total_price: Decimal = Decimal(0)+ currency: str = "USD"+ latency: float = 0.0+ time_to_first_token: float | None = None+ time_to_generate: float | None = Nonemodel_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")diff --git a/dify-agent/src/dify_agent/runtime/runner.py b/dify-agent/src/dify_agent/runtime/runner.pyindex 315f4484c4b..8fec8d5fbf8 100644--- a/dify-agent/src/dify_agent/runtime/runner.py+++ b/dify-agent/src/dify_agent/runtime/runner.py@@ -38,6 +38,7 @@ from dataclasses import dataclassfrom typing import Any, Literal, Protocol, cast, runtime_checkableimport httpx+from graphon.model_runtime.entities.llm_entities import LLMUsagefrom pydantic import JsonValue, TypeAdapterfrom pydantic_ai.exceptions import ModelHTTPErrorfrom pydantic_ai.messages import AgentStreamEvent, PartDeltaEvent, PartStartEvent, TextPart, TextPartDelta@@ -91,17 +92,23 @@ class _HasUsage(Protocol):@runtime_checkableclass _HasInputTokens(Protocol):- input_tokens: object+ input_tokens: int | None@runtime_checkableclass _HasOutputTokens(Protocol):- output_tokens: object+ output_tokens: int | None@runtime_checkableclass _HasTotalTokens(Protocol):- total_tokens: object+ total_tokens: int | None+++@runtime_checkable+class _HasAccumulatedUsage(Protocol):+ @property+ def accumulated_usage(self) -> LLMUsage | None: ...class AgentRunValidationError(ValueError):@@ -365,7 +372,8 @@ class AgentRunRunner:deferred_tool_results=deferred_tool_results,event_stream_handler=handle_events,)- usage = _serialize_agent_usage(_result_usage(result))+ complete_usage = model.accumulated_usage if isinstance(model, _HasAccumulatedUsage) else None+ usage = _serialize_agent_usage(complete_usage if complete_usage is not None else _result_usage(result))append_successful_run_history(history_layer, result.new_messages())if isinstance(result.output, DeferredToolRequests):if ask_human_layer is None:@@ -424,9 +432,11 @@ def _result_usage(result: object) -> object | None:def _serialize_agent_usage(usage: object | None) -> AgentRunUsage | None:- """Convert pydantic-ai request usage into the public Agent run usage shape."""+ """Convert complete daemon or fallback pydantic-ai usage into the public shape."""if usage is None:return None+ if isinstance(usage, LLMUsage):+ return AgentRunUsage.model_validate(usage.model_dump(mode="python"))input_tokens = int(usage.input_tokens or 0) if isinstance(usage, _HasInputTokens) else 0output_tokens = int(usage.output_tokens or 0) if isinstance(usage, _HasOutputTokens) else 0total_tokens = int(usage.total_tokens or 0) if isinstance(usage, _HasTotalTokens) else 0diff --git a/dify-agent/tests/local/dify_agent/adapters/llm/_test_support.py b/dify-agent/tests/local/dify_agent/adapters/llm/_test_support.pyindex 20bb7d734be..1b5d41aa320 100644--- a/dify-agent/tests/local/dify_agent/adapters/llm/_test_support.py+++ b/dify-agent/tests/local/dify_agent/adapters/llm/_test_support.py@@ -12,21 +12,40 @@ from graphon.model_runtime.entities.message_entities import ()from pydantic import BaseModel+_ZERO_PRICE = Decimal(0)-def make_usage(prompt_tokens: int = 3, completion_tokens: int = 5) -> LLMUsage:++def make_usage(+ prompt_tokens: int = 3,+ completion_tokens: int = 5,+ *,+ prompt_unit_price: Decimal = _ZERO_PRICE,+ prompt_price_unit: Decimal = _ZERO_PRICE,+ prompt_price: Decimal = _ZERO_PRICE,+ completion_unit_price: Decimal = _ZERO_PRICE,+ completion_price_unit: Decimal = _ZERO_PRICE,+ completion_price: Decimal = _ZERO_PRICE,+ total_price: Decimal = _ZERO_PRICE,+ currency: str = "USD",+ latency: float = 0.1,+ time_to_first_token: float | None = None,+ time_to_generate: float | None = None,+) -> LLMUsage:return LLMUsage(prompt_tokens=prompt_tokens,- prompt_unit_price=Decimal("0"),- prompt_price_unit=Decimal("0"),- prompt_price=Decimal("0"),+ prompt_unit_price=prompt_unit_price,+ prompt_price_unit=prompt_price_unit,+ prompt_price=prompt_price,completion_tokens=completion_tokens,- completion_unit_price=Decimal("0"),- completion_price_unit=Decimal("0"),- completion_price=Decimal("0"),+ completion_unit_price=completion_unit_price,+ completion_price_unit=completion_price_unit,+ completion_price=completion_price,total_tokens=prompt_tokens + completion_tokens,- total_price=Decimal("0"),- currency="USD",- latency=0.1,+ total_price=total_price,+ currency=currency,+ latency=latency,+ time_to_first_token=time_to_first_token,+ time_to_generate=time_to_generate,)diff --git a/dify-agent/tests/local/dify_agent/adapters/llm/test_model.py b/dify-agent/tests/local/dify_agent/adapters/llm/test_model.pyindex fac1d79d994..163f47af8ff 100644--- a/dify-agent/tests/local/dify_agent/adapters/llm/test_model.py+++ b/dify-agent/tests/local/dify_agent/adapters/llm/test_model.py@@ -1,6 +1,7 @@import jsonimport unittestfrom contextlib import asynccontextmanager+from decimal import Decimalfrom typing import castfrom unittest.mock import patch@@ -187,6 +188,89 @@ class DifyLLMAdapterModelTests(unittest.IsolatedAsyncioTestCase):self.assertEqual(response.parts[0].part_kind, "text")self.assertEqual(cast(TextPart, response.parts[0]).content, "adapter response")+ async def test_request_accumulates_complete_plugin_usage_across_model_rounds(self) -> None:+ usages = [+ make_usage(+ prompt_tokens=10,+ completion_tokens=2,+ prompt_unit_price=Decimal("5"),+ prompt_price_unit=Decimal("0.000001"),+ prompt_price=Decimal("0.000050"),+ completion_unit_price=Decimal("30"),+ completion_price_unit=Decimal("0.000001"),+ completion_price=Decimal("0.000060"),+ total_price=Decimal("0.000110"),+ latency=0.4,+ time_to_first_token=0.1,+ time_to_generate=0.3,+ ),+ make_usage(+ prompt_tokens=20,+ completion_tokens=3,+ prompt_unit_price=Decimal("5"),+ prompt_price_unit=Decimal("0.000001"),+ prompt_price=Decimal("0.000100"),+ completion_unit_price=Decimal("30"),+ completion_price_unit=Decimal("0.000001"),+ completion_price=Decimal("0.000090"),+ total_price=Decimal("0.000190"),+ latency=0.8,+ time_to_first_token=0.2,+ time_to_generate=0.6,+ ),+ ]+ request_count = 0++ def handler(_request: httpx.Request) -> httpx.Response:+ nonlocal request_count+ usage = usages[request_count]+ request_count += 1+ return build_stream_response(+ LLMResultChunk(+ model="demo-model",+ delta=LLMResultChunkDelta(+ index=0,+ message=AssistantPromptMessage(content="done", tool_calls=[]),+ usage=usage,+ ),+ )+ )++ async with self.mock_daemon_stream(httpx.MockTransport(handler)):+ adapter = DifyLLMAdapterModel(+ "demo-model",+ self.make_provider(),+ model_provider="openai",+ credentials={"api_key": "secret"},+ )+ _ = await adapter.request(+ [ModelRequest(parts=[UserPromptPart("first")])],+ model_settings=None,+ model_request_parameters=ModelRequestParameters(),+ )+ async with adapter.request_stream(+ [ModelRequest(parts=[UserPromptPart("second")])],+ model_settings=None,+ model_request_parameters=ModelRequestParameters(),+ ) as stream:+ events = [event async for event in stream]++ usage = adapter.accumulated_usage+ self.assertEqual(request_count, 2)+ self.assertTrue(events)+ self.assertIsNotNone(usage)+ assert usage is not None+ self.assertEqual(usage.prompt_tokens, 30)+ self.assertEqual(usage.completion_tokens, 5)+ self.assertEqual(usage.total_tokens, 35)+ self.assertEqual(usage.prompt_price, Decimal("0.000150"))+ self.assertEqual(usage.completion_price, Decimal("0.000150"))+ self.assertEqual(usage.total_price, Decimal("0.000300"))+ self.assertEqual(usage.currency, "USD")+ self.assertAlmostEqual(usage.latency, 1.2)+ self.assertEqual(usage.time_to_first_token, 0.2)+ self.assertEqual(usage.time_to_generate, 0.6)+async def test_request_maps_tool_call_only_assistant_history_to_empty_string_content(self) -> None:messages = [ModelRequest(parts=[SystemPromptPart("request system"), UserPromptPart("hello")]),diff --git a/dify-agent/tests/local/dify_agent/protocol/test_protocol_schemas.py b/dify-agent/tests/local/dify_agent/protocol/test_protocol_schemas.pyindex be5ac5be9a5..9aa8cf8af0f 100644--- a/dify-agent/tests/local/dify_agent/protocol/test_protocol_schemas.py+++ b/dify-agent/tests/local/dify_agent/protocol/test_protocol_schemas.py@@ -1,3 +1,5 @@+from decimal import Decimal+import pytestfrom pydantic import ValidationErrorfrom pydantic_ai.messages import FinalResultEvent@@ -429,6 +431,45 @@ def test_run_succeeded_event_round_trips_usage() -> None:assert b'"usage"' in payload+def test_run_succeeded_event_round_trips_complete_pricing_usage() -> None:+ event = RunSucceededEvent(+ run_id="run-priced-usage",+ data=RunSucceededEventData(+ output="done",+ session_snapshot=CompositorSessionSnapshot(layers=[]),+ usage=AgentRunUsage(+ prompt_tokens=10,+ prompt_unit_price=Decimal("5"),+ prompt_price_unit=Decimal("0.000001"),+ prompt_price=Decimal("0.000050"),+ completion_tokens=2,+ completion_unit_price=Decimal("30"),+ completion_price_unit=Decimal("0.000001"),+ completion_price=Decimal("0.000060"),+ total_tokens=12,+ total_price=Decimal("0.000110"),+ currency="USD",+ latency=0.4,+ time_to_first_token=0.1,+ time_to_generate=0.3,+ ),+ ),+ )++ payload = RUN_EVENT_ADAPTER.dump_json(event)+ decoded = RUN_EVENT_ADAPTER.validate_json(payload)++ assert isinstance(decoded, RunSucceededEvent)+ assert decoded.data.usage is not None+ assert decoded.data.usage.prompt_price == Decimal("0.000050")+ assert decoded.data.usage.completion_price == Decimal("0.000060")+ assert decoded.data.usage.total_price == Decimal("0.000110")+ assert decoded.data.usage.currency == "USD"+ assert decoded.data.usage.latency == 0.4+ assert decoded.data.usage.time_to_first_token == 0.1+ assert decoded.data.usage.time_to_generate == 0.3++def test_on_exit_accept_layer_overrides() -> None:request = CreateRunRequest.model_validate({diff --git a/dify-agent/tests/local/dify_agent/runtime/test_runner.py b/dify-agent/tests/local/dify_agent/runtime/test_runner.pyindex 07886a9dfa2..dceab28ab98 100644--- a/dify-agent/tests/local/dify_agent/runtime/test_runner.py+++ b/dify-agent/tests/local/dify_agent/runtime/test_runner.py@@ -1,9 +1,11 @@import asynciofrom collections.abc import Iterable, Mapping+from decimal import Decimalfrom typing import Any, ClassVar, castimport httpximport pytest+from graphon.model_runtime.entities.llm_entities import LLMUsagefrom pydantic import JsonValuefrom pydantic_ai import Toolfrom pydantic_ai.exceptions import ModelHTTPError, UnexpectedModelBehavior@@ -514,6 +516,62 @@ def test_runner_emits_terminal_success_and_snapshot(monkeypatch: pytest.MonkeyPaassert sink.statuses["run-1"] == "succeeded"+def test_runner_emits_complete_plugin_usage_in_terminal_event(monkeypatch: pytest.MonkeyPatch) -> None:+ complete_usage = LLMUsage(+ prompt_tokens=10,+ prompt_unit_price=Decimal("5"),+ prompt_price_unit=Decimal("0.000001"),+ prompt_price=Decimal("0.000050"),+ completion_tokens=2,+ completion_unit_price=Decimal("30"),+ completion_price_unit=Decimal("0.000001"),+ completion_price=Decimal("0.000060"),+ total_tokens=12,+ total_price=Decimal("0.000110"),+ currency="USD",+ latency=0.4,+ time_to_first_token=0.1,+ time_to_generate=0.3,+ )++ class PricedTestModel(TestModel):+ @property+ def accumulated_usage(self) -> LLMUsage:+ return complete_usage++ def fake_get_model(_self: DifyPluginLLMLayer, *, http_client: httpx.AsyncClient):+ assert http_client.is_closed is False+ return PricedTestModel(custom_output_text="done") # pyright: ignore[reportReturnType]++ monkeypatch.setattr(DifyPluginLLMLayer, "get_model", fake_get_model)+ sink = InMemoryRunEventSink()++ async def scenario() -> None:+ async with httpx.AsyncClient() as client:+ await AgentRunRunner(+ sink=sink,+ request=_request(),+ run_id="run-priced",+ plugin_daemon_http_client=client,+ dify_api_http_client=client,+ ).run()++ asyncio.run(scenario())++ terminal = sink.events["run-priced"][-1]+ assert isinstance(terminal, RunSucceededEvent)+ assert terminal.data.usage is not None+ assert terminal.data.usage.prompt_unit_price == Decimal("5")+ assert terminal.data.usage.prompt_price == Decimal("0.000050")+ assert terminal.data.usage.completion_unit_price == Decimal("30")+ assert terminal.data.usage.completion_price == Decimal("0.000060")+ assert terminal.data.usage.total_price == Decimal("0.000110")+ assert terminal.data.usage.currency == "USD"+ assert terminal.data.usage.latency == 0.4+ assert terminal.data.usage.time_to_first_token == 0.1+ assert terminal.data.usage.time_to_generate == 0.3++def test_runner_preserves_explicit_json_null_output(monkeypatch: pytest.MonkeyPatch) -> None:def fake_get_model(_self: DifyPluginLLMLayer, *, http_client: httpx.AsyncClient):assert http_client.is_closed is False