Documentation
¶
Overview ¶
Package aigateway is an AI gateway for togo — a control plane in front of LLM providers (Cortex-style). It wraps completions with per-key rate limiting and spend caps, response caching, prompt-template management, and provider routing/fallback, so the AI kit can be exposed safely to many consumers.
Providers implement the small Provider interface (an adapter bridges the togo `ai` plugin's drivers), so the gateway stays decoupled and testable offline.
Index ¶
- Variables
- func RegisterProvider(p Provider)
- type Gateway
- func (g *Gateway) Complete(ctx context.Context, rawKey string, req Request) (Response, error)
- func (g *Gateway) CreateKey(name string, k Key) (raw string, rec *Key)
- func (g *Gateway) Keys() []*Key
- func (g *Gateway) Prompts() *promptStore
- func (g *Gateway) RenderPrompt(name string, vars map[string]string) (string, bool)
- func (g *Gateway) Revoke(id string) bool
- func (g *Gateway) SetPrice(model string, p Price)
- func (g *Gateway) Usage(keyID string) []UsageRecord
- type Key
- type Message
- type Price
- type Provider
- type Request
- type Response
- type UsageRecord
Constants ¶
This section is empty.
Variables ¶
var ( ErrKeyInvalid = errors.New("ai-gateway: invalid or revoked API key") ErrRateLimited = errors.New("ai-gateway: rate limit exceeded") ErrQuotaExceeded = errors.New("ai-gateway: spend quota exceeded") ErrModelNotAllowed = errors.New("ai-gateway: model not allowed for this key") ErrNoProvider = errors.New("ai-gateway: no provider could handle the request") )
Errors returned by the gateway.
Functions ¶
func RegisterProvider ¶
func RegisterProvider(p Provider)
RegisterProvider adds a provider to the routing chain (order = priority).
Types ¶
type Gateway ¶
type Gateway struct {
// contains filtered or unexported fields
}
Gateway is the runtime stored on the kernel (k.Get("ai-gateway")).
func (*Gateway) Complete ¶
Complete is the gateway entrypoint: authenticate the key, enforce rate limit + spend caps, serve from cache, route to a provider (with fallback), bill, cache.
func (*Gateway) CreateKey ¶
CreateKey mints an API key, returning the raw key once (only its hash is kept).
func (*Gateway) Prompts ¶
func (g *Gateway) Prompts() *promptStore
Prompts exposes the prompt-template store.
func (*Gateway) RenderPrompt ¶
RenderPrompt renders a stored template's active version with vars.
func (*Gateway) Usage ¶
func (g *Gateway) Usage(keyID string) []UsageRecord
Usage returns recorded usage, optionally filtered by key id.
type Key ¶
type Key struct {
ID string `json:"id"`
Name string `json:"name"`
AllowedModels []string `json:"allowed_models,omitempty"`
RatePerMin int `json:"rate_per_min"` // 0 = unlimited
MonthlyCap float64 `json:"monthly_cap"` // USD, 0 = unlimited
HardCap float64 `json:"hard_cap"`
Spend float64 `json:"spend"`
CreatedAt time.Time `json:"created_at"`
Revoked bool `json:"revoked"`
// contains filtered or unexported fields
}
Key is an API key with per-key limits + accumulated spend.
type Provider ¶
type Provider interface {
Name() string
Complete(ctx context.Context, req Request) (Response, error)
}
Provider is implemented by an LLM backend (or an adapter over the togo ai kit).
type Request ¶
type Request struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Temperature float64 `json:"temperature,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
}
Request is a completion request.
type Response ¶
type Response struct {
Content string `json:"content"`
Model string `json:"model"`
Provider string `json:"provider,omitempty"`
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
Cached bool `json:"cached"`
Cost float64 `json:"cost"`
}
Response is a completion result.