Commit Graph
441 Commits
Author SHA1 Message Date
lukaszraczylo 19c6cfed03 docs(metrics): say the provider health gauge is shared with sharedState
The HELP text claimed the discovery breaker is never shared via Redis.
With redis.sharedState on it is shared, so every replica reports the
same value and max() applies. The golden scrape changes by this line.
2026-11-26 04:14:53 +00:00
lukaszraczylo d90514cd55 perf(limits): add fleet latency counters only with a store
accountWith appended the fleet latency sums even for an in-memory
limiter, which nothing reads. It now appends them only when a store is
configured.
2026-11-26 04:14:08 +00:00
lukaszraczylo 0753a8cb4c fix(shared-state): read calibrator counters without the limiter latch
The shared calibration refresh read through the limiter. A Redis error
there latched the chat route's limiter store for five seconds. The
refresh now reads the store directly, as the shared-state loop does.
2026-11-26 04:13:36 +00:00
lukaszraczylo 367f3b60ff perf(shared-state): batch calibrator counters with the usage write
Each served request spawned a second async write for the calibrator
counters. They now ride the batch accountWith already sends, and only
when shared state is on and a store is configured.
2026-11-26 04:12:38 +00:00
lukaszraczylo 383d477778 fix(shared-state): rate-limit the loop's store error logs
A Redis outage logged one line per refresh and per failed write. The
loop now logs at most one store error per 30 seconds, the interval the
limiter already uses.
2026-11-26 04:11:17 +00:00
lukaszraczylo a9468f5d51 fix(shared-state): drop breaker deltas older than the fail window
During a Redis outage the queued failure deltas kept a replaced
gateway's loop alive for ever. A queued failure older than the fail
window cannot change the fleet count, so the breaker now drops it.
2026-11-26 04:10:29 +00:00
lukaszraczylo 5cb01e2c4b fix(shared-state): restart the loop when an in-flight request ends
A request that outlived the idle window ended after the loop had
stopped. Its finish delta then stayed queued with no loop to send it.
The end function now touches the loop.
2026-11-26 04:09:07 +00:00
lukaszraczylo 6438a95a76 fix(shared-state): resend an ambiguous in-flight batch at most once
A flush whose reply timed out may have landed in Redis. The flusher
kept the batch and resent it every tick, which counted it many times.
A batch that never reached the wire is still kept. A batch that may
have landed is resent once and then dropped.
2026-11-26 04:07:43 +00:00
lukaszraczylo b1fa1bee14 feat(resp): flag pipeline failures that never reached the wire
A pipeline that failed before it wrote its request cannot have changed
the server. The error now says so, and respNotApplied reads that flag.
It walks Unwrap by hand because yaegi has no errors.As.
2026-11-26 04:06:34 +00:00
lukaszraczylo 648848e23a fix(shared-state): trip the breaker after a lost incr reply
A flush whose INCR Redis applied but whose reply failed was resent.
The resend never crossed the threshold, so the fleet never tripped.
The flush now trips when the count is at or above the threshold and the
last refresh shows the breaker closed. A short reply is now an error.
2026-11-26 04:05:30 +00:00
lukaszraczylo 3833997221 fix(shared-state): reset breaker backoff when a request succeeds
The open count only grew, so each trip after a recovery waited longer
than the last. The breaker now reads the open count on every refresh.
A success sets the count to zero when the last refresh showed it above
zero. The next trip then opens for the base duration.
2026-11-26 04:04:21 +00:00
lukaszraczylo eba8b67526 docs: shared state across replicas
Document redis.sharedState: the five shared states, their key prefixes, the refresh delay, the Redis outage fallback and the rolling toggle undercount. Update the metrics notes for the store-backed families.
2026-11-26 03:48:14 +00:00
lukaszraczylo 4c25cfc546 test(yaegi): exercise shared breaker and in-flight under yaegi
The probe trips a shared breaker through one interpreted instance over an in-memory store. It checks that a second instance sees the breaker open after one refresh and sees it closed after a success. It also sums in-flight counts across two instances. A mismatch fails make yaegi-check.
2026-11-26 03:43:01 +00:00
lukaszraczylo e70eb32336 feat(router): share the token-size estimate through Redis
With redis.sharedState on, each observed sample also adds to day counters of prompt bytes, prompt tokens and sample count per model. The router-learning loop reads the counters of the routers' target models and keeps one fleet ratio per model. The estimate prefers that ratio once the fleet has the same minimum sample count as the local calibrator. Dropped counter writes only blur the estimate. With the switch off, the calibrator runs its local code only and makes no store call.
2026-11-26 03:32:03 +00:00
lukaszraczylo fe5e9afdc9 feat(router): share the decision breaker through Redis
The decision-model breaker now also reads and writes the fleet breaker when redis.sharedState is on. Routers that use the same decision provider share one key. A client cancel is still not a failure. With the switch off, the decider runs its local code only and makes no store call.
2026-11-26 03:29:55 +00:00
lukaszraczylo 0c2445cb1a feat(registry): share the discovery breaker through Redis
A provider whose discovery fails often enough now reads as unhealthy on every pod when redis.sharedState is on. The shared breaker uses the same threshold and open times as the local one. The discovered model sets stay local. With the switch off, the provider state runs its local code only and makes no store call.
2026-11-26 03:29:09 +00:00
lukaszraczylo a6c2821a29 feat(concurrency): share in-flight counts through Redis
In-flight counts and the learned limit now merge the fleet view when redis.sharedState is on. The load a pod reports to the router and the saturation downgrade is the larger of its own count and the fleet count. A 429 halves the fleet count, not only the local one. With the switch off, the learner runs its local code only and makes no store call.
2026-11-26 03:28:07 +00:00
lukaszraczylo a46058694b feat(failover): share the request breaker through Redis
The request breaker now also reads and writes the fleet breaker when redis.sharedState is on. A pod that sees enough failures opens the breaker for every pod after one refresh. A success on any pod closes it again. With the switch off, the tracker runs its local code only and makes no store call.
2026-11-26 03:26:39 +00:00
lukaszraczylo 807cc6ee75 fix(redis): flush in-flight counts from the refresh loop
begin and end now add to a local accumulator. The refresh loop sends
all deltas in one incrMulti before each read, so a request starts no
goroutine and makes no store call. A failed flush keeps the deltas.
Deltas for epochs outside the read window are discarded.

Breaker failures use the same path. The instance whose flush takes a
count from below the threshold to at or above it opens the breaker.
The loop does not idle-stop with pending deltas and flushes on Close.
2026-11-26 03:22:50 +00:00
lukaszraczylo 590a23750b feat(redis): shared breaker and in-flight primitives
Add sharedStateLoop, sharedBreaker and sharedInflight. One loop per
gateway refreshes every primitive from the store once per second, so
request paths read local copies only. A store error keeps the last
values, and three failed refreshes make shared values absent.

Add the redis.sharedState switch. It defaults off, and it needs
redis.address. With it off, no shared keys are read or written.
2026-11-26 03:15:12 +00:00
lukaszraczylo 50d97a3221 fix(metrics): fold provenance into account batch, pin no-store output
Provenance counters now ride the accountWith batch. They no longer use a separate droppable async write, so a request costs the same number of store round trips as before.

A golden file pins the full no-store scrape body. It was captured at the base commit, so any change to the no-store output fails the test.

A partial store failure test shows that a failed family read latches the store and skips later reads in the same scrape. The README notes the rollout-day stream label and the removal of rejection series.
2026-11-26 02:45:51 +00:00
lukaszraczylo 6d55972b60 fix(metrics): render fleet-wide families from the shared store
The /metrics route runs in its own instance. That instance handles no chat traffic, so its in-memory families stayed empty.

With a limiter store, /metrics now reads the router, decision, latency, prompt-cache, provenance and rejection families from shared counters. Their HELP text uses the store-backed aggregation note. Without a store, or without the stats flag a family needs, the in-memory rendering is unchanged.

The latency counters gain day-window sums in milliseconds and streaming-only bucket counters. The histograms can then emit a real _sum and keep the stream label. Provenance writes use countAsync. The rejection month counter rides the existing rejection batch. A store read failure skips the affected families.
2026-11-26 02:34:38 +00:00
lukaszraczylo 0ffd022a9f test(yaegi): check prompt-cache cost, injection and key under yaegi 2026-11-26 02:02:50 +00:00
lukaszraczylo cf1ae262d6 refactor(pricing): share one prompt token split 2026-11-26 01:55:17 +00:00
lukaszraczylo ee758d5d09 perf(limits): write prompt-cache counters for the model scope only 2026-11-26 01:55:06 +00:00
lukaszraczylo 2e687e90d9 fix(admin): read prompt-cache counters for returned rows only 2026-11-26 01:54:23 +00:00
lukaszraczylo 86870668ea fix(anthropic): keep system a string when marked parts are empty 2026-11-26 01:53:51 +00:00
lukaszraczylo 1618934cf7 docs: correct prompt-cache cost clamp wording
The clamp bounds token counts only. A cache write price above the input price can raise the cost above the all-uncached cost.
2026-11-26 01:43:32 +00:00
lukaszraczylo acf53491cc docs: prompt caching
Document the prompt-caching feature in the README. Add promptCache key to the openai provider in the catalog testData, and update the manifest test mirror.
2026-11-26 01:42:13 +00:00
lukaszraczylo 2fe798ad82 fix(openai): forward prompt_cache_key on translated messages only when enabled
The translated messages path copied any prompt_cache_key into the
upstream body, even with promptCache.key off. It now forwards the key
only when the provider opted in. The gateway key travels in a private
field, and a client key wins when present.
2026-11-26 01:37:52 +00:00
lukaszraczylo cff1761a21 feat(openai): opt-in per-conversation prompt_cache_key
When promptCache.key is set on an openai provider, the gateway sends a
hashed prompt_cache_key per conversation. The key is the SHA-256 of the
caller id and the session id. The session id is the client id, or the
system prompt plus the first user message.

The key is set only when the client sent no prompt_cache_key. It is set
on each attempt copy after the response-cache key is computed. It is not
stored, and it never carries a raw id.
2026-11-26 01:33:56 +00:00
lukaszraczylo 5ffd44874c feat(anthropic): opt-in rolling prompt-cache breakpoints
Add providers.<name>.promptCache with inject and key flags. When inject is true on an anthropic provider, the gateway adds up to four cache_control breakpoints. They go on the last tool, the last system block, and the last two user messages. The translated chat path and the /v1/messages passthrough both use it. A body that already has any cache_control is left unchanged. The input body is never mutated. The default config changes no request body.
2026-11-26 01:28:51 +00:00
lukaszraczylo bc77e16e45 fix(anthropic): keep client cache_control on translated system messages
The translator joined system and developer messages into one string.
This dropped any cache_control marker that the client set on a part.
Now, when a marker exists, system becomes a list of text blocks that
keep the marker. Without markers, the joined string is unchanged.
2026-11-26 01:22:08 +00:00
lukaszraczylo 8a530c1cb3 fix(admin): mirror backend detail parsing in the mock API
The mock now parses the detail query like the backend. Only 1 and true turn detail on. Other unknown values return 400. detail=0 and detail=false no longer return the prompt-cache fields.
2026-11-26 01:18:06 +00:00
lukaszraczylo 90ad08920a build(admin): regenerate embedded admin UI
Run make admin-ui to embed the prompt-cache hit rate UI.
2026-11-26 01:15:59 +00:00
lukaszraczylo c14ce25ce0 feat(admin): show prompt-cache hit rate per model
The usage model table and the model panel now show the prompt-cache hit rate. The Cost avoided card shows the prompt-cache saving as a separate line. The mock admin API fixtures carry the new fields.
2026-11-26 01:15:54 +00:00
lukaszraczylo fd8ca1e95a fix(admin): keep the legacy usage models shape for prompt-cache fields
The three prompt-cache fields are now optional pointers. Only the detail
view sets them, so the default response stays the same as before.
2026-11-26 01:08:48 +00:00
lukaszraczylo d661166eab feat(metrics): count prompt-cache tokens and savings per model
The gateway now counts cache-read, cache-write and uncached prompt tokens
per provider and model as a Prometheus counter. It also writes the read,
write and saved micro-USD totals to the Redis counters.

The admin usage models detail view returns the three totals per model.
Response-cache hits do not count, and free models save nothing.
2026-11-26 01:03:50 +00:00
lukaszraczylo 8817157efc fix(pricing): fill flagship cache prices from the generated table
A curated builtinPricing entry returned before the generated table, so its cache prices were never reached.
lookupPricingSource now fills nil cache fields of a curated hit from the generated table.
Base prices are unchanged, and an operator override is not merged.
The clamp test now asserts exact values.
Tests that compared prices with == now use reflect.DeepEqual, because the price struct holds pointers.
2026-11-26 00:54:38 +00:00
lukaszraczylo 66506dfff2 chore(pricing): add cache prices to builtin table
The base prices and the entry set are unchanged.
113 entries gain cache-read or cache-write prices.
The cache fields come from LiteLLM on 2026-11-26.
A full pricing-sync run is not used, because it would drop 36 entries.
2026-11-26 00:49:21 +00:00
lukaszraczylo 55ebe1fae1 feat(pricing): price cached and cache-write input tokens
Cost now prices cache-read and cache-write prompt tokens at their own rates.
A model without a cache price keeps the input rate, so cost does not change.
The cache shares are clamped to the prompt, so odd usage cannot give a negative cost.
LiteLLM field names cache_read_input_token_cost and cache_creation_input_token_cost were confirmed in the fetched source.
The pricing-sync tool reads both fields. The table regeneration is a separate commit.
2026-11-26 00:37:19 +00:00
lukaszraczylo 73ff7d92e0 feat(usage): parse cache-read and cache-write tokens for all providers 2026-11-26 00:29:39 +00:00
lukaszraczylo dc6daeea1d chore(plugin): exercise the decision block in catalog testData
The auto router gets a shadow decision block and a provider with a decision path prefix. The manifest test mirrors both and runs New() on them.
2026-11-25 14:18:49 +00:00
lukaszraczylo bf529326b7 docs: decision model assist details and admin panel features
Document the decision request text, the live skip, the provider type, the saturation rule, and the redesigned admin pages.
2026-11-25 14:16:12 +00:00
lukaszraczylo e6605dce6d test(router): widen decision timeout test margin
The server delay is 2s and the test bound is 1s. The 50ms timeout stays.
2026-11-25 14:16:04 +00:00
lukaszraczylo 21e9bc84d1 fix(router): send the head of the system prompt and ask to the decision model
The decision state now holds the first 1024 bytes of the system prompt and the first 8192 bytes of the newest ask, cut at a character boundary. The classifier scoring inputs stay the same.
2026-11-25 14:15:51 +00:00
lukaszraczylo 2cd828c162 fix(router): decision call skips settled requests and ignores client cancels
Live mode no longer calls the decision model when a floor or keyword rule already fixes the tier. A client cancel during the call no longer counts toward the breaker.
2026-11-25 14:15:20 +00:00
lukaszraczylo 8c01f15ee5 docs: smart router decision model assist 2026-11-25 14:02:26 +00:00
lukaszraczylo 3008748681 build(admin): regenerate embedded admin UI 2026-11-25 14:00:32 +00:00
lukaszraczylo 5d4a9e6cad feat(admin): decision model grid in router panel 2026-11-25 13:59:39 +00:00