Ask the endpoint what a streamed reply cost
A streamed completion carries no token counts unless you ask for them, and
`stream_options: {include_usage: true}` is how. Not every server implements
it, and an unknown key is a 400 from some -- the same hazard as sending a
tools array to an endpoint without support. So it is asked for once per base
URL per process, and an endpoint that refuses is remembered and retried
without it. The retry is safe because the status is checked before a single
line is read: nothing has been yielded, so there is nothing to duplicate.
chunk_usage() reads the resulting chunk. It needed no change to the loop
above it: a usage chunk carries `choices: []`, which is exactly the shape
delta_text, delta_reasoning, delta_tool_calls and finish_reason have always
returned early on. All-zero counts are treated as absent, because some
servers attach zeros to every chunk and the real numbers only at the end.
services/tokens.py is the fallback for endpoints that never report: four
characters to a token, counting the tools array because thirteen schemas is
a meaningful slice of a short window, and counting nothing for an image
because its cost depends on tiling and an invented number would be worse
than the omission. Crude on purpose -- a real tokeniser means one per model
family, for a figure that is displayed beside a tilde.
Nothing uses any of this yet.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+139
-1
@@ -6,8 +6,9 @@ from fastapi.testclient import TestClient
|
||||
from sqlalchemy import select
|
||||
|
||||
from lembas.db.models import Connection, Model
|
||||
from lembas.services import tokens
|
||||
from lembas.services.crypto import encrypt
|
||||
from lembas.services.llm.openai_client import context_from
|
||||
from lembas.services.llm.openai_client import chunk_usage, context_from
|
||||
|
||||
|
||||
def _model(db, **kwargs) -> Model:
|
||||
@@ -120,3 +121,140 @@ def test_junk_in_the_context_length_field_is_ignored_not_a_500(
|
||||
assert response.status_code == 303
|
||||
db.refresh(model)
|
||||
assert model.context_length == 4096
|
||||
|
||||
|
||||
# --- Usage off the wire -------------------------------------------------------
|
||||
def test_usage_is_read_from_a_usage_chunk():
|
||||
chunk = {
|
||||
"choices": [],
|
||||
"usage": {"prompt_tokens": 100, "completion_tokens": 20, "total_tokens": 120},
|
||||
}
|
||||
assert chunk_usage(chunk) == {
|
||||
"prompt_tokens": 100,
|
||||
"completion_tokens": 20,
|
||||
"total_tokens": 120,
|
||||
}
|
||||
|
||||
|
||||
def test_a_missing_total_is_worked_out():
|
||||
chunk = {"choices": [], "usage": {"prompt_tokens": 100, "completion_tokens": 20}}
|
||||
assert chunk_usage(chunk)["total_tokens"] == 120
|
||||
|
||||
|
||||
def test_an_ordinary_chunk_carries_no_usage():
|
||||
assert chunk_usage({"choices": [{"delta": {"content": "hi"}}]}) is None
|
||||
assert chunk_usage({}) is None
|
||||
assert chunk_usage({"usage": "lots"}) is None
|
||||
|
||||
|
||||
def test_an_all_zero_usage_object_is_not_an_answer():
|
||||
"""Some servers attach zeros to every chunk and the real numbers only at the
|
||||
end. Believing the zeros freezes the count at nothing."""
|
||||
chunk = {"choices": [], "usage": {"prompt_tokens": 0, "completion_tokens": 0}}
|
||||
assert chunk_usage(chunk) is None
|
||||
|
||||
|
||||
def test_the_other_accessors_still_ignore_a_usage_chunk():
|
||||
"""They return early on `choices: []`, which is exactly the shape of one.
|
||||
That is what lets a usage chunk through the loop untouched."""
|
||||
from lembas.services.llm.openai_client import (
|
||||
delta_reasoning,
|
||||
delta_text,
|
||||
delta_tool_calls,
|
||||
finish_reason,
|
||||
)
|
||||
|
||||
chunk = {"choices": [], "usage": {"prompt_tokens": 1, "completion_tokens": 1}}
|
||||
assert delta_text(chunk) == ""
|
||||
assert delta_reasoning(chunk) == ""
|
||||
assert delta_tool_calls(chunk) == []
|
||||
assert finish_reason(chunk) == ""
|
||||
|
||||
|
||||
async def test_stream_options_is_asked_for(mock_http):
|
||||
import json as json_module
|
||||
|
||||
import httpx
|
||||
|
||||
from lembas.services.llm.openai_client import Endpoint, stream_chat
|
||||
|
||||
seen: list[dict] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
seen.append(json_module.loads(request.content))
|
||||
return httpx.Response(200, text="data: [DONE]\n\n")
|
||||
|
||||
mock_http(handler)
|
||||
async for _ in stream_chat(Endpoint("http://ask.test", "", {}), {"model": "m"}):
|
||||
pass
|
||||
|
||||
assert seen[0]["stream_options"] == {"include_usage": True}
|
||||
|
||||
|
||||
async def test_an_endpoint_that_rejects_stream_options_is_asked_once(mock_http):
|
||||
"""A 400 for an unknown key is the same hazard as sending `tools` to an
|
||||
endpoint without support. Retry without it, then stop asking."""
|
||||
import json as json_module
|
||||
|
||||
import httpx
|
||||
|
||||
from lembas.services.llm.openai_client import (
|
||||
_NO_STREAM_OPTIONS,
|
||||
Endpoint,
|
||||
stream_chat,
|
||||
)
|
||||
|
||||
_NO_STREAM_OPTIONS.discard("http://fussy.test")
|
||||
seen: list[dict] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
body = json_module.loads(request.content)
|
||||
seen.append(body)
|
||||
if "stream_options" in body:
|
||||
return httpx.Response(400, json={"error": {"message": "unknown field"}})
|
||||
reply = 'data: {"choices":[{"delta":{"content":"hi"}}]}\n\ndata: [DONE]\n\n'
|
||||
return httpx.Response(200, text=reply)
|
||||
|
||||
mock_http(handler)
|
||||
endpoint = Endpoint("http://fussy.test", "", {})
|
||||
|
||||
text = [c async for c in stream_chat(endpoint, {"model": "m"})]
|
||||
assert text, "the retry should have produced the reply"
|
||||
assert len(seen) == 2
|
||||
|
||||
# Second reply: it already knows, so one request and no stream_options.
|
||||
async for _ in stream_chat(endpoint, {"model": "m"}):
|
||||
pass
|
||||
assert len(seen) == 3
|
||||
assert "stream_options" not in seen[2]
|
||||
_NO_STREAM_OPTIONS.discard("http://fussy.test")
|
||||
|
||||
|
||||
# --- The estimate -------------------------------------------------------------
|
||||
def test_the_estimate_is_about_four_characters_a_token():
|
||||
assert tokens.estimate("") == 0
|
||||
assert tokens.estimate("x" * 400) == 100
|
||||
|
||||
|
||||
def test_typed_content_parts_are_counted_and_images_are_not():
|
||||
"""An image's cost depends on the model's tiling. A number invented here
|
||||
would be worse than the omission."""
|
||||
content = [
|
||||
{"type": "text", "text": "x" * 40},
|
||||
{"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA" * 500}},
|
||||
]
|
||||
assert tokens.estimate_content(content) == 10
|
||||
|
||||
|
||||
def test_a_request_estimate_includes_the_tools_array():
|
||||
"""Thirteen schemas is a meaningful slice of a short window; leaving them
|
||||
out would read low exactly when it matters."""
|
||||
payload = {
|
||||
"messages": [{"role": "user", "content": "x" * 40}],
|
||||
"tools": [
|
||||
{"function": {"name": "web_search", "description": "y" * 400, "parameters": {}}}
|
||||
],
|
||||
}
|
||||
assert tokens.estimate_request(payload) > tokens.estimate_request(
|
||||
{"messages": payload["messages"]}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user