Documentation
¶
Overview ¶
Package llmproxy is muster's LLM proxy for officer instances: an Anthropic-Messages-shaped endpoint (POST /llm/v1/messages on the connect listener, internal/api) that a routine officer instance's Claude Code points ANTHROPIC_BASE_URL at, and that forwards to one provider (OpenRouter's Anthropic-compatible POST /api/v1/messages) with the officer's key.
Per request, in order: the body is read and rebuilt from an allowlist of Anthropic Messages fields (forwardBodyFields), its `model` set to the officer's pinned model (the client's is ignored); the officer's spend this UTC day is read from the store and the request refused once it reaches the daily budget; the instance's tokens on its held item are checked against the per-item cap; the request goes upstream with the key attached; the response is relayed event by event (server-sent events are flushed as each arrives); the usage and cost the provider reported in the stream are booked to the store before the response ends, so the instance's next request sees them; and one audit record and one log line are written.
🔴 THE KEY IS ATTACHED HERE AND NOWHERE ELSE. It is never logged, never audited, never sent to the client: every byte relayed downstream is passed through redact first, a 401/403 body is replaced by muster's own message, and every other non-2xx body is relayed redacted. The client's own credential (its muster token) is never sent upstream.
🔴 THE MODEL PIN HOLDS BECAUSE THE BODY IS AN ALLOWLIST. OpenRouter reads routing fields in the body (fallbacks, models, provider, route, plugins, transforms) that can serve a request from a model other than the pinned one. Only the Anthropic Messages fields in forwardBodyFields go upstream; every other key is dropped.
🔴 COST IS THE PROVIDER'S, NOT A PRICE LIST'S. OpenRouter reports what it billed in the stream (`usage.cost` in message_delta). Billed prices have run well above list prices, so only a response that carries no cost is booked from an estimate, at deliberately pessimistic rates (EstimateUSDPerInputToken, EstimateUSDPerOutputToken), and counted as estimated.
Index ¶
Constants ¶
const ( EstimateUSDPerInputToken = 0.60e-6 EstimateUSDPerOutputToken = 1.20e-6 )
Pessimistic $/token for a response that reported no cost. NOT list prices: they are set above the highest billed rate observed for the pinned models, so an estimate over-books rather than under-books.
const DefaultUpstream = "https://openrouter.ai/api"
DefaultUpstream is the provider's Anthropic-compatible API base; requests go to DefaultUpstream + UpstreamPath.
const (
// MaxRequestBytes bounds a request body (a long session's whole context).
MaxRequestBytes = 32 << 20
)
Limits.
const UpstreamPath = "/v1/messages"
UpstreamPath is the one provider path the proxy calls. Claude Code calls only POST /v1/messages (with ?beta=true) against its base URL, always streamed; no count_tokens or model-list call was observed. Every other path under /llm/ is refused by the route layer.
Variables ¶
This section is empty.
Functions ¶
func Refuse ¶
func Refuse(w http.ResponseWriter, status int, errType, msg string)
Refuse writes an Anthropic-shaped error, which Claude Code shows as the turn's API error.
⚠ Status choice matters to the client: Claude Code retries 429 and 5xx with backoff (and a 401 about ten times), so a refusal that must END the turn — the budget, the item cap — is a 400, never a 429.
Types ¶
type Caller ¶
type Caller struct {
Officer string
// Instance is the routine instance's number in its pool, and
// InstanceLabel how it is named in metrics and logs (`r<n>`).
Instance int
InstanceLabel string
AgentID int64
AgentName string
Model string
DailyBudgetUSD float64
MaxTokensPerItem int64
Key string
}
Caller is the officer instance a request is made for, with its officer's settings. The route layer builds it after authenticating the instance.
type Config ¶
type Config struct {
// Upstream is the provider's API base ("" is DefaultUpstream).
Upstream string
// HTTP is the upstream transport; nil is a client that follows no
// redirect.
HTTP *http.Client
// Store is required.
Store Store
// Audit receives one record per request ServeMessages answers (a
// malformed body's refusal included); nil records none.
Audit func(ctx context.Context, r Record)
Logger *log.Logger
// Now is the clock; nil is time.Now.
Now func() time.Time
}
Config is New's input.
type PGStore ¶
type PGStore struct {
// contains filtered or unexported fields
}
PGStore is Store on officer_llm_usage (migration 0075), under row-level security: ctx must act as the officer's owner (the request's principal is the calling instance's owner, which is its officer's), and the row lands under that owner. Numbers only: no column can hold a prompt, a response or a key.
type Proxy ¶
type Proxy struct {
// contains filtered or unexported fields
}
Proxy is the LLM proxy.
func (*Proxy) ServeMessages ¶
ServeMessages proxies one POST /llm/v1/messages for c.
type Record ¶
type Record struct {
Officer string
Instance string // the instance's label, `r<n>`
AgentID int64
AgentName string
Item string
RequestedModel string
Model string
Provider string
MessageID string
Status int
Result string
Input int64
Output int64
CacheRead int64
CacheWrite int64
CostUSD float64
CostEstimated bool
StopReason string
Duration time.Duration
}
Record is one request's audit record. It carries no body, no header and no credential.
type Store ¶
type Store interface {
// SpentToday is officer's booked USD on day, over every instance.
SpentToday(ctx context.Context, officer string, day time.Time) (float64, error)
// Book adds u (one request) to its (officer, instance, day) row.
Book(ctx context.Context, u Usage) error
}
Store persists each officer's usage per instance and UTC day, so a restart does not reset a budget.