diff --git a/README.md b/README.md index c7d8408..f720a50 100644 --- a/README.md +++ b/README.md @@ -556,9 +556,11 @@ and the log can never disagree. prices per million tokens. The billed name is the target's `as` model; a `#variant` suffix is a selector, not part of the name, so `deepseek-v4.1-flash#max` uses the base `deepseek-v4.1-flash` row. An exact row for the full variant still -wins. For an endpoint with `kind opencode-go`, a `-go` row wins over the base -row: it describes the subscription (zero token price, budget/requests) rather than -the pay-as-you-go list price. +wins. A `-go` row is used only when the model name itself is `...-go`; the endpoint +kind does not silently change the price. The endpoint's `offpeak` window is +evaluated at request time, so `offpeak=IN,OUT` columns apply even when the endpoint +has no usage command. Chunked non-streaming bodies are dechunked before usage is +read, so they are costed too. A non-streaming response carries `X-Junction-Tokens-In`, `X-Junction-Tokens-Out` and, when the price is known, `X-Junction-Cost-Usd`. A reviewed answer puts the @@ -566,10 +568,13 @@ sum over the worker and all rounds in both `X-Junction-Review-Cost-Usd` and the general `X-Junction-Cost-Usd`, so one header is enough. An unpriced model keeps `unknown`; without any price table the headers are not invented. A cache hit is a call that cost nothing: it reports cost `0.000000` and zero tokens even though the -cached body still contains the provider's original usage. The spend log and -`/spend` are authoritative for every call, including streams; a stream cannot get a -late cost header because its head is already on the wire, but a reviewed stream -carries the summed cost in its final `junction` object. +cached body still contains the provider's original usage. + +A plain stream cannot be costed after the fact: its head is already on the wire and +junction does not parse usage out of an arbitrary SSE stream, so its ledger line has +zeros rather than a guess. A reviewed stream is buffered, so it carries the summed +cost in its final `junction` object. The spend log and `/spend` remain authoritative +for non-streaming calls and for reviewed streams. ## Virtual keys, limits, spend diff --git a/config.example b/config.example index cc8e5c7..cf69568 100644 --- a/config.example +++ b/config.example @@ -249,8 +249,8 @@ req path "/v1/models" check none pass peer1, peer2, opencode-go # first-call provider 4xx reaches the client with its own status and body. # Optional price table: model input cached output [offpeak=IN,OUT] [budget=USD] -# A "#variant" uses the base model's row; kind opencode-go prefers "-go" -# (subscription: 0 token price) over the list price. Non-streaming responses then +# A "#variant" uses the base model's row; a "-go" name uses its own row. The +# endpoint's offpeak window is evaluated per request. Non-streaming responses then # carry X-Junction-Tokens-In/-Out and X-Junction-Cost-Usd. # prices /home/lotherk/stack/data/junction/prices.conf diff --git a/src/junction.c b/src/junction.c index 71f13c3..8af02e0 100644 --- a/src/junction.c +++ b/src/junction.c @@ -3104,7 +3104,9 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq, /* Verbrauch lässt sich nur zählen, wenn die Antwort ganz vorliegt. Bei gesetzter * Preistabelle wird deshalb auch ohne Cache vollständig gelesen — ein nicht * gestreamter JSON-Rumpf ist ohnehin endlich. */ - int want_usage = (j_price_count() > 0) && !r.chunked && (r.status == 200); + /* A non-streaming JSON body may arrive chunked too. read_full_body() + * dechunks it, so the usage block is parseable either way. */ + int want_usage = (j_price_count() > 0) && (r.status == 200) && !head_sent; /* Auch eine chunked Modellliste wird gelesen und gefiltert: eine Politik, die * ein Anbieter mit Transfer-Encoding umgehen kann, ist keine Politik. */ int want_models_rewrite = (r.status == 200 && @@ -3143,14 +3145,18 @@ static int forward_step(j_conn *client, j_endpoint *ep, const j_req *rq, * Kopfzeilen und im /status, nicht nur im Log. */ { long u_in = 0, u_out = 0, u_cached = 0; - if (!r.chunked && usage_from_body(body, body_len, &u_in, &u_out, &u_cached)) { + if (usage_from_body(body, body_len, &u_in, &u_out, &u_cached)) { l_in = u_in; l_out = u_out; l_cached = u_cached; const char *billed = (as_model && as_model[0]) ? as_model : rq_model; - double cost = j_price_cost_kind(billed, ep->kind, u_in, u_cached, u_out, - ep->offpeak_active); + /* Respect the endpoint's off-peak window at request time, not the + * possibly minutes-old value from the usage poller — and not only + * for endpoints that happen to have a usage_cmd. */ + int offpeak = ep_in_offpeak(ep); + double cost = j_price_cost(billed, u_in, u_cached, u_out, offpeak); STATE_LOCK(); + ep->offpeak_active = offpeak; ep->tokens_in += (unsigned long)u_in; ep->tokens_out += (unsigned long)u_out; ep->tokens_cached += (unsigned long)u_cached; @@ -3408,8 +3414,7 @@ static void review_usage_add(review_usage *u, const char *body, const char *mode u->out += out; u->cached += cached; u->seen = 1; - double cost = j_price_cost_kind(model, ep->kind, in, cached, out, - ep->offpeak_active); + double cost = j_price_cost(model, in, cached, out, ep_in_offpeak(ep)); if (cost < 0) u->cost_known = 0; else u->cost += cost; } diff --git a/src/price.c b/src/price.c index 50fcf7b..41dad48 100644 --- a/src/price.c +++ b/src/price.c @@ -118,7 +118,7 @@ static const j_price *price_star(void) return NULL; } -int j_price_lookup_kind(const char *model, const char *kind, j_price *out) +int j_price_lookup(const char *model, j_price *out) { if (!model || !*model) return -1; @@ -131,11 +131,6 @@ int j_price_lookup_kind(const char *model, const char *kind, j_price *out) base[b] = '\0'; const j_price *hit = price_exact(model); - if (!hit && kind && !strcmp(kind, "opencode-go") && base[0]) { - char go[J_PRICE_NAME + 4]; - int n = snprintf(go, sizeof go, "%s-go", base); - if (n > 0 && (size_t)n < sizeof go) hit = price_exact(go); - } if (!hit && base[0]) hit = price_exact(base); if (!hit) hit = price_star(); if (!hit) return -1; @@ -143,16 +138,11 @@ int j_price_lookup_kind(const char *model, const char *kind, j_price *out) return 0; } -int j_price_lookup(const char *model, j_price *out) -{ - return j_price_lookup_kind(model, NULL, out); -} - -double j_price_cost_kind(const char *model, const char *kind, long in_tokens, - long cached_tokens, long out_tokens, int offpeak) +double j_price_cost(const char *model, long in_tokens, long cached_tokens, long out_tokens, + int offpeak) { j_price p; - if (j_price_lookup_kind(model, kind, &p) != 0) return -1.0; + if (j_price_lookup(model, &p) != 0) return -1.0; double pi = p.in, po = p.out; if (offpeak && p.has_offpeak) { pi = p.off_in; po = p.off_out; } @@ -163,9 +153,3 @@ double j_price_cost_kind(const char *model, const char *kind, long in_tokens, double fresh = (double)(in_tokens - cached_tokens); return (fresh * pi + (double)cached_tokens * p.cached + (double)out_tokens * po) / 1e6; } - -double j_price_cost(const char *model, long in_tokens, long cached_tokens, long out_tokens, - int offpeak) -{ - return j_price_cost_kind(model, NULL, in_tokens, cached_tokens, out_tokens, offpeak); -} diff --git a/src/price.h b/src/price.h index ba54d35..111336e 100644 --- a/src/price.h +++ b/src/price.h @@ -23,18 +23,13 @@ int j_price_count(void); /* 0 = gefunden (exakter Name vor "*"), -1 = unbekannt. Ein unbekannter Preis ist * keine Null — der Unterschied entscheidet, ob man dem Bericht glauben darf. - * Ein "#variante"-Suffix wird ignoriert: der Basisname ist bepreist. */ + * Ein "#variante"-Suffix wird ignoriert: der Basisname ist bepreist. Eine Zeile + * für den vollen Namen (auch "-go") gewinnt, wenn sie existiert. */ int j_price_lookup(const char *model, j_price *out); -/* Wie oben, aber mit Endpoint-Art. Für kind "opencode-go" gewinnt eine - * "-go"-Zeile: sie beschreibt das Abo (0 Tokenpreis, Budget/Requests) - * statt des Listenpreises. Eine exakte Zeile für den vollen Namen gewinnt immer. */ -int j_price_lookup_kind(const char *model, const char *kind, j_price *out); - -/* Kosten in US-Dollar. -1.0, wenn das Modell keinen Preis hat. */ +/* Kosten in US-Dollar. -1.0, wenn das Modell keinen Preis hat. `offpeak` wendet + * die offpeak=-Spalten der Zeile an; der Aufrufer bestimmt das Fenster. */ double j_price_cost(const char *model, long in_tokens, long cached_tokens, long out_tokens, int offpeak); -double j_price_cost_kind(const char *model, const char *kind, long in_tokens, - long cached_tokens, long out_tokens, int offpeak); #endif diff --git a/tests/run.sh b/tests/run.sh index 45344f0..ead9698 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -71,7 +71,7 @@ log_has() { # answer requests with somebody else's config), so fail loudly and early. # 8157 gehört auf dieser Maschine dem Hermes-Gateway — der Port wird von keinem # Test benutzt, also darf er die Suite nicht aufhalten. -for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 8204 8205 8206 8207 8208 8209 9443 9990 9991 9992 9993 9994 9995 9996; do +for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 8204 8205 8206 8207 8208 8209 8210 9443 9990 9991 9992 9993 9994 9995 9996; do if ss -tln 2>/dev/null | grep -q ":$port "; then echo "port $port is busy — a leftover junction/stub is still running:" ss -tlnp 2>/dev/null | grep ":$port " | sed 's/^/ /' diff --git a/tests/usage.sh b/tests/usage.sh index 1b3af41..fb0dec3 100644 --- a/tests/usage.sh +++ b/tests/usage.sh @@ -30,8 +30,10 @@ sleep 0.6 cat > "$TMP/prices.conf" <<'CFG' # test/preis in cached out test/preis 0.30 0.006 1.20 -# Subscription row: 0 token price, only a budget/request window. +# A -go row exists but the model name asks for test/preis (no -go suffix), so it +# must not be selected just because the endpoint kind is opencode-go. test/preis-go 0 0 0 budget=10 requests=100/5h +test/offpeak 0.30 0.006 1.20 offpeak=0.15,0.60 CFG cat > "$TMP/usage.conf" < "$TMP/offpeak.conf" < "$TMP/offpeak.log" 2>&1 & +PIDS+=($!) +wait_port "$OFFPORT" 30 || bad "the off-peak instance did not come up" +OFFBODY='{"model":"test/offpeak","messages":[{"role":"user","content":"hi"}],"max_tokens":50}' +OFFCOST=$(hdr "$OFFBODY" "Cost-Usd" "http://127.0.0.1:$OFFPORT/v1/chat/completions") +[ "$OFFCOST" = "0.000216" ] && ok "off-peak prices are applied from the endpoint window" \ + || bad "off-peak cost was '${OFFCOST:-none}', expected 0.000216" # Ein Cache-Hit kostet nichts; der gespeicherte Rumpf trägt aber noch die Usage # des ursprünglichen Calls. Kopfzeilen und Ledger müssen den Hit mit 0 zeigen.