#!/bin/bash # Wird von tests/run.sh eingebunden: Verbrauch und Kosten pro Anfrage. echo "== 24. Verbrauch und Kosten pro Anfrage ==" UPORT=$((PORT+71)) USPORT=$((PORT+72)) cat > "$TMP/ustub.py" <<'PY' import http.server, json, sys class H(http.server.BaseHTTPRequestHandler): def log_message(self, *a): pass def do_POST(self): n = int(self.headers.get("Content-Length", "0")) self.rfile.read(n) out = json.dumps({"id": "u", "choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}}], "usage": {"prompt_tokens": 1200, "completion_tokens": 300, "prompt_tokens_details": {"cached_tokens": 1000}}}).encode() self.send_response(200) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(out))) self.end_headers() self.wfile.write(out) http.server.HTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever() PY python3 "$TMP/ustub.py" "$USPORT" & PIDS+=($!) sleep 0.6 cat > "$TMP/prices.conf" <<'CFG' # test/preis in cached out test/preis 0.30 0.006 1.20 # 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/usage.log" 2>&1 & PIDS+=($!) sleep 0.8 hdr() { # hdr [url] local url=${3:-"http://127.0.0.1:$UPORT/v1/chat/completions"} curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$1" \ "$url" | tr -d '\r' \ | sed -n "s/^[Xx]-[Jj]unction-${2}: //p" } PRICED='{"model":"test/preis","messages":[{"role":"user","content":"hi"}],"max_tokens":50}' # Eine Anfrage, alle Kopfzeilen — sonst zählt der Test drei Aufrufe statt einem. ALL=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$PRICED" \ "http://127.0.0.1:$UPORT/v1/chat/completions" | tr -d '\r') IN=$(printf '%s' "$ALL" | sed -n 's/^[Xx]-[Jj]unction-[Tt]okens-[Ii]n: //p') OUT=$(printf '%s' "$ALL" | sed -n 's/^[Xx]-[Jj]unction-[Tt]okens-[Oo]ut: //p') COST=$(printf '%s' "$ALL" | sed -n 's/^[Xx]-[Jj]unction-[Cc]ost-[Uu]sd: //p') [ "$IN" = "1200" ] && ok "input tokens are reported in the response headers" \ || bad "input tokens missing (got '${IN:-none}')" [ "$OUT" = "300" ] && ok "output tokens are reported in the response headers" \ || bad "output tokens missing (got '${OUT:-none}')" # 1000 von 1200 Eingaben waren zwischengespeichert: 200*0.30 + 1000*0.006 + 300*1.20, # alles durch eine Million. WANT="0.000426" [ "$COST" = "$WANT" ] && ok "cost uses the cached-input price where the provider reports it" \ || bad "cost was '${COST:-none}', expected $WANT" UNKNOWN='{"model":"gibt-es-nicht","messages":[{"role":"user","content":"hi"}],"max_tokens":50}' UCOST=$(hdr "$UNKNOWN" "Cost-Usd") [ "$UCOST" = "unknown" ] && ok "a model without a price reports unknown, not zero" \ || bad "unpriced model reported '${UCOST:-none}'" STATUS=$(curl -s -m 5 "http://127.0.0.1:$UPORT/status") case "$STATUS" in *'"cost_unknown":1'*) ok "/status counts the unpriced request separately" ;; *) bad "/status does not count unpriced requests" ;; esac case "$STATUS" in *'"tokens_in":2400'*) ok "/status accumulates tokens across requests" ;; *) bad "/status token totals are wrong" ;; esac # Ohne Preistabelle darf gar nichts gerechnet werden — nur gezählt. NOPRICE=$((PORT+69)) cat > "$TMP/noprice.conf" < "$TMP/noprice.log" 2>&1 & PIDS+=($!) wait_port "$NOPRICE" 30 || bad "the no-price instance did not come up" NP=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$PRICED" \ "http://127.0.0.1:$NOPRICE/v1/chat/completions" | tr -d '\r' | sed -n 's/^[Xx]-[Jj]unction-[Cc]ost-Usd: //p') [ -z "$NP" ] && ok "without a price table no cost header is invented" \ || bad "a cost was reported without any price table ('$NP')" # Ein "#variant" ist ein Selektor, kein eigener Modellname: der Basispreis gilt. VARIANT='{"model":"test/preis#max","messages":[{"role":"user","content":"hi"}],"max_tokens":50}' VCOST=$(hdr "$VARIANT" "Cost-Usd") [ "$VCOST" = "0.000426" ] && ok "a #variant uses the base model's price" \ || bad "variant cost was '${VCOST:-none}', expected 0.000426" # Ein opencode-go-Endpoint nutzt die -go-Zeile (Abo: 0 Tokenpreis), nicht den # Listenpreis der Basiszeile. GOPORT=$((PORT+70)) cat > "$TMP/go.conf" < "$TMP/go.log" 2>&1 & PIDS+=($!) wait_port "$GOPORT" 30 || bad "the opencode-go cost instance did not come up" GOCOST=$(hdr "$VARIANT" "Cost-Usd" "http://127.0.0.1:$GOPORT/v1/chat/completions") [ "$GOCOST" = "0.000426" ] && ok "the endpoint kind does not change the model's price" \ || bad "opencode-go cost was '${GOCOST:-none}', expected 0.000426" # Off-Peak: das Zeitfenster des Endpoints wird beim Request ausgewertet, nicht # nur vom Usage-Poller. 200 frische * 0.15 + 1000 cached * 0.006 + 300 * 0.60. OFFPORT=$((PORT+75)) cat > "$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. CACHEPORT=$((PORT+74)) cat > "$TMP/cost-cache.conf" < "$TMP/cost-cache.log" 2>&1 & PIDS+=($!) wait_port "$CACHEPORT" 30 || bad "the cost-cache instance did not come up" CACHED='{"model":"test/preis","messages":[{"role":"user","content":"cache cost"}],"max_tokens":50,"temperature":0}' C1=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$CACHED" \ "http://127.0.0.1:$CACHEPORT/v1/chat/completions" | tr -d '\r') C2=$(curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$CACHED" \ "http://127.0.0.1:$CACHEPORT/v1/chat/completions" | tr -d '\r') C1COST=$(printf '%s' "$C1" | sed -n 's/^[Xx]-[Jj]unction-[Cc]ost-[Uu]sd: //p') C2COST=$(printf '%s' "$C2" | sed -n 's/^[Xx]-[Jj]unction-[Cc]ost-[Uu]sd: //p') C2IN=$(printf '%s' "$C2" | sed -n 's/^[Xx]-[Jj]unction-[Tt]okens-[Ii]n: //p') C2OUT=$(printf '%s' "$C2" | sed -n 's/^[Xx]-[Jj]unction-[Tt]okens-[Oo]ut: //p') [ "$C1COST" = "0.000426" ] && ok "the first priced call reports its exact cost" \ || bad "first cached call cost was '${C1COST:-none}'" [ "$C2COST" = "0.000000" ] && ok "a cache hit reports cost 0, not the original cost" \ || bad "cache hit cost was '${C2COST:-none}'" [ "$C2IN" = "0" ] && [ "$C2OUT" = "0" ] && ok "a cache hit reports zero tokens" \ || bad "cache hit tokens were '${C2IN:-none}'/'${C2OUT:-none}'"