Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(serve): /v1/systemone/batch total_usage sums output_tokens, not a hardcoded 0

AlKor136f45a8699005
diff --git a/laya/serve.py b/laya/serve.py
index 12254fae..eb82df85 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -993,10 +993,11 @@ def create_app(router: Optional[Any] = None):
for s in states]
else:
results = [router.predict(s, questions, model=model) for s in states]
- total_tokens = sum(r.get("usage", {}).get("input_tokens", 0) for r in results)
+ total_in = sum(r.get("usage", {}).get("input_tokens", 0) for r in results)
+ total_out = sum(r.get("usage", {}).get("output_tokens", 0) for r in results)
return {
"results": results,
- "total_usage": {"input_tokens": total_tokens, "output_tokens": 0},
+ "total_usage": {"input_tokens": total_in, "output_tokens": total_out},
}
t0 = time.perf_counter()
diff --git a/tests/test_serve.py b/tests/test_serve.py
index 155aeffb..d1a4c126 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -1188,6 +1188,26 @@ def test_batch_happy_path(monkeypatch):
assert "X-Inference-Time-Ms" in r.headers
+def test_batch_sums_output_tokens():
+ """total_usage must sum output_tokens, not report a hardcoded 0 (each result carries its own)."""
+ class _UsageRouter:
+ def predict(self, state, questions, model=None, **kwargs):
+ return {"model": "laya-rl-agent",
+ "answers": {"dept": {"type": "choice", "choice": "billing",
+ "probabilities": {"billing": 1.0}, "confidence": 1.0}},
+ "usage": {"input_tokens": 5, "output_tokens": 3},
+ "routing": {"model": "english"}}
+
+ client = TestClient(create_app(router=_UsageRouter()))
+ r = client.post("/v1/systemone/batch", json=BATCH_REQ)
+ assert r.status_code == 200, r.text
+ data = r.json()
+ assert data["total_usage"]["output_tokens"] == sum(
+ res["usage"]["output_tokens"] for res in data["results"])
+ assert data["total_usage"]["output_tokens"] == 6 # two states x 3 each
+ assert data["total_usage"]["input_tokens"] == 10
+
+
def test_batch_missing_state_in_list_returns_400(monkeypatch):
"""A None state inside states list must be rejected with 400 'state' is required."""
client, _ = _client(monkeypatch)