Files
LocalAI/core/http/middleware/usage_stamp.go
Richard Palethorpe 6a80e23733 feat(middleware): Model routing, PII filtering, Cloud model proxies (#9802)
Add a routing middleware stack and a cloud-proxy backend.

* cloud-proxy: a Go gRPC backend that forwards OpenAI- and
  Anthropic-shaped chat requests to upstream providers, with an
  optional translate mode (OpenAI request -> Anthropic /v1/messages
  -> OpenAI response) and full tool-calling support.

* routing: admission control, content-aware model routing
  (embedding cache + classifier + rerank + Arch-Router score),
  PII detection/redaction (regex + NER) with streaming filter and
  OpenAI/Anthropic adapters, and a per-user/per-key billing recorder
  backed by GORM or in-memory storage.

* middleware: UsageMiddleware records usage via the billing recorder,
  plus admission, route-model, usage-stamp and trace middlewares.

* observability: BackendTrace ring buffer stores full request bodies
  (capped), MITM proxy emits structured trace events, and router
  classifier decisions surface at /api/router/decide.

* gallery: Arch-Router-1.5B (Q4_K_M and Q8_0).

* UI: cloud-proxy model-editor fields, classifier system-prompt and
  score-normalization config, and a Traces page rendering request
  bodies.

Assisted-by: claude-code:claude-opus-4-7 [Read] [Edit] [Bash]

Signed-off-by: Richard Palethorpe <io@richiejp.com>
2026-05-25 09:28:27 +02:00

34 lines
1.2 KiB
Go

package middleware
import "github.com/labstack/echo/v4"
// StampUsage records the canonical token counts on the echo context so
// UsageMiddleware can attribute the request without parsing the response
// body. Handlers must call this for every successful response — the
// body-parse fallback is reserved for foreign endpoints (e.g., the cloud
// passthrough proxy).
//
// model is the name written into the response payload; passing it here
// is what lets the middleware fill the UsageRecord even when the handler
// abbreviates or rewrites the user-supplied model. Empty values are
// ignored so partial information is still useful (e.g., embeddings calls
// where completion is always 0).
//
// prompt and completion accept int because that's the native width of
// LocalAI's TokenUsage / OpenAIUsage structs (token counts never come
// close to overflow). Conversion to int64 happens once, here, so call
// sites stay free of casts.
func StampUsage(c echo.Context, model string, prompt, completion int) {
if c == nil {
return
}
if model != "" {
c.Set(ContextKeyResponseModel, model)
}
p := int64(prompt)
cp := int64(completion)
c.Set(ContextKeyPromptTokens, p)
c.Set(ContextKeyCompletionTokens, cp)
c.Set(ContextKeyTotalTokens, p+cp)
}