litellm/litellm-rust/loadtest/cmd/genconfig/main.go
narmstrong-berry 25064726cc
feat(gateway): serve /v1/decisions
Port the proxy's OpenAI-format Decisions endpoint to the Rust gateway at /v1/decisions and
/decisions, for OpenAI and Azure OpenAI deployments. Requests are validated as the proxy
validates them before hooks count them, the provider body is rebuilt from the decision fields,
and answers are aligned to the questions with refusals for what the provider left out. Box
questions follow model_info.supports_box_questions and drop_params.

Decisions run through the hooks pipeline like chat and messages: virtual keys and allowed routes,
rate limits, budgets, spend logs, prometheus and the S3 log. Cost uses the cost map's
_decisions price overrides on the decisions route.

The loadtest kit gains decisions support in mockllm, loadgen and genconfig, and a
system-one-auto workload of 2,000 input and 20 output tokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013FDwtmKWH21oDCJn1MFbd3
2026-11-11 15:38:40 +00:00

198 lines
9 KiB
Go

// genconfig writes a gateway config with many model groups and deployments pointing at mockllm,
// plus a set of virtual keys:
//
// config.yaml model_list + router_settings + master key
// keys.txt one virtual key per line, for loadgen (keys-<set>.txt with -key-sets)
// keys.sql the same keys, hashed, as rows of LiteLLM_VerificationToken
//
// Deployment api_bases carry /d/<id> so mockllm can count hits and inject faults per deployment.
//
// -features full turns on everything the gateways enforce and record per request, set so nothing
// is refused at load-test rates: every key belongs to a user and a team (with a membership budget)
// and every owner carries rpm, tpm and budget limits; deployments carry rpm and tpm for model
// capacity; and the config lists the dynamic_rate_limiter_v3, prometheus and s3_v2 callbacks, the
// Rust gateway's request hooks and their Python stand-ins, written beside the config as
// custom_callbacks.py. headers.txt (headers-<set>.txt) holds the request headers those hooks act
// on. S3 settings come from LOADTEST_S3_* environment variables.
//
// -key-sets python,rust writes one key set, with its own users and teams, per name, so each
// gateway's spend can be told apart in a shared database.
package main
import (
"bufio"
"flag"
"fmt"
"log"
"math"
"os"
"path/filepath"
"strings"
)
func main() {
mocks := flag.String("mocks", "http://127.0.0.1:9000", "comma-separated mockllm base URLs; deployments are spread across them")
chatModel := flag.String("chat-model", "gpt-6-astra", "upstream model for chat deployments; groups are named <model>-0, <model>-1, ...")
messagesModel := flag.String("messages-model", "claude-opus-5-5", "upstream model for messages deployments; groups are named <model>-0, ...")
bedrockModel := flag.String("bedrock-model", "us.anthropic.claude-opus-5-5", "Bedrock model id for the Bedrock share of messages deployments")
bedrockFraction := flag.Float64("bedrock-fraction", 0.5, "share of each messages group's deployments served through Bedrock invoke (SigV4-signed, AWS event-stream responses) instead of the Anthropic API; 0 for Anthropic only")
chatGroups := flag.Int("chat-groups", 4, "chat model groups")
messagesGroups := flag.Int("messages-groups", 4, "messages model groups")
decisionsModel := flag.String("decisions-model", "gpt-6-luna", "upstream model for decisions deployments; groups are named <model>-0, <model>-1, ...")
decisionsGroups := flag.Int("decisions-groups", 0, "decisions model groups (the gateway's /v1/decisions; Bifrost gets none)")
decisionsProvider := flag.String("decisions-provider", "openai", "provider prefix for decisions deployments: openai or azure")
deployments := flag.Int("deployments", 4, "deployments per model group")
chatProvider := flag.String("chat-provider", "openai", "provider prefix for chat deployments (openai is served natively, streaming included; openai_like is non-streaming only)")
keys := flag.Int("keys", 1000, "virtual keys to generate")
keySeed := flag.String("key-seed", "", "derive keys from this seed so every machine generates the same set (default: random keys)")
masterKey := flag.String("master-key", "sk-loadtest-master", "gateway master key")
outDir := flag.String("out", ".", "output directory")
features := flag.String("features", "basic", "basic (virtual keys only) or full (limits, budgets, callbacks and hooks)")
keySets := flag.String("key-sets", "", "comma-separated key set names, each written to keys-<name>.txt with its own users and teams (default: one set in keys.txt); the set named bifrost becomes bifrost-config.json")
bifrostConcurrency := flag.Int("bifrost-concurrency", 500, "Bifrost workers per deployment (each deployment is a provider with its own worker pool)")
bifrostBuffer := flag.Int("bifrost-buffer", 5000, "Bifrost queue length per deployment, and its client pool size")
bifrostBedrock := flag.String("bifrost-bedrock-tls", "127.0.0.1:9443", "host:port of mockllm -tls-addr, where Bifrost's Bedrock deployments go")
bifrostPricing := flag.String("bifrost-pricing-file", "/config/bifrost-pricing.json", "where the Bifrost gateway finds bifrost-pricing.json")
flag.Parse()
if *decisionsProvider != "openai" && *decisionsProvider != "azure" {
log.Fatalf("-decisions-provider must be openai or azure, got %q", *decisionsProvider)
}
var full bool
switch *features {
case "basic":
case "full":
full = true
default:
log.Fatalf("-features must be basic or full, got %q", *features)
}
if err := os.MkdirAll(*outDir, 0o755); err != nil {
log.Fatal(err)
}
bases := strings.Split(*mocks, ",")
var cfg strings.Builder
cfg.WriteString("# Generated by loadtest/cmd/genconfig. Every deployment points at mockllm.\nmodel_list:\n")
n := 0
var bifrostDeps []bifrostDeployment
deployment := func(group, id, model, apiBase string) {
kind, upstream, _ := strings.Cut(model, "/")
bifrostDeps = append(bifrostDeps, bifrostDeployment{id: id, group: group, kind: kind, model: upstream,
base: strings.TrimSuffix(apiBase, "/v1")})
fmt.Fprintf(&cfg, " - model_name: %s\n litellm_params:\n model: %s\n api_base: %s\n api_key: sk-mock\n",
group, model, apiBase)
if full {
cfg.WriteString(deploymentCapacity)
}
n++
}
// Bedrock deployments carry static AWS keys instead of an api_key (which Bedrock reads as a
// bearer token), so every request goes through SigV4 signing as it does with IAM credentials.
bedrockDeployment := func(group, id, model, apiBase string) {
bifrostDeps = append(bifrostDeps, bifrostDeployment{id: id, group: group, kind: "bedrock",
model: strings.TrimPrefix(model, "bedrock/"), base: apiBase})
fmt.Fprintf(&cfg, " - model_name: %s\n litellm_params:\n model: %s\n api_base: %s\n"+
" aws_access_key_id: mock-loadtest-access-key\n aws_secret_access_key: mock-loadtest-secret\n aws_region_name: us-east-1\n",
group, model, apiBase)
if full {
cfg.WriteString(deploymentCapacity)
}
n++
}
for g := range *chatGroups {
for i := range *deployments {
base := strings.TrimRight(bases[n%len(bases)], "/")
id := fmt.Sprintf("chat-%d-%d", g, i)
deployment(fmt.Sprintf("%s-%d", *chatModel, g), id, *chatProvider+"/"+*chatModel, fmt.Sprintf("%s/d/%s/v1", base, id))
}
}
bedrockPerGroup := int(math.Round(*bedrockFraction * float64(*deployments)))
for g := range *messagesGroups {
for i := range *deployments {
base := strings.TrimRight(bases[n%len(bases)], "/")
group := fmt.Sprintf("%s-%d", *messagesModel, g)
if i < bedrockPerGroup {
id := fmt.Sprintf("bedrock-%d-%d", g, i)
bedrockDeployment(group, id, "bedrock/"+*bedrockModel, fmt.Sprintf("%s/d/%s", base, id))
continue
}
id := fmt.Sprintf("msg-%d-%d", g, i)
deployment(group, id, "anthropic/"+*messagesModel, fmt.Sprintf("%s/d/%s", base, id))
}
}
for g := range *decisionsGroups {
for i := range *deployments {
base := strings.TrimRight(bases[n%len(bases)], "/")
id := fmt.Sprintf("dec-%d-%d", g, i)
apiBase := fmt.Sprintf("%s/d/%s", base, id)
if *decisionsProvider == "openai" {
apiBase += "/v1"
}
fmt.Fprintf(&cfg, " - model_name: %s-%d\n litellm_params:\n model: %s/%s\n api_base: %s\n api_key: sk-mock\n",
*decisionsModel, g, *decisionsProvider, *decisionsModel, apiBase)
if full {
cfg.WriteString(deploymentCapacity)
}
n++
}
}
fmt.Fprintf(&cfg, `router_settings:
routing_strategy: simple-shuffle
num_retries: 2
allowed_fails: 3
cooldown_time: 30
general_settings:
master_key: %s
`, *masterKey)
if full {
cfg.WriteString(fullFeatureSettings)
write(filepath.Join(*outDir, "custom_callbacks.py"), customCallbacks)
}
write(filepath.Join(*outDir, "config.yaml"), cfg.String())
sets := []string{""}
if *keySets != "" {
sets = strings.Split(*keySets, ",")
}
sf, err := os.Create(filepath.Join(*outDir, "keys.sql"))
if err != nil {
log.Fatal(err)
}
sw := bufio.NewWriter(sf)
sw.WriteString("-- Load-test virtual keys. Hash = sha256 hex of the key, as the proxy stores it.\n")
for _, set := range sets {
if set == bifrostSet {
writeBifrost(bifrostDeps, bifrostOptions{full: full, keys: *keys, keySeed: *keySeed,
concurrency: *bifrostConcurrency, bufferSize: *bifrostBuffer, pricingFile: *bifrostPricing, bedrockTLS: *bifrostBedrock, outDir: *outDir})
continue
}
name := "keys.txt"
if set != "" {
name = "keys-" + set + ".txt"
}
kf, err := os.Create(filepath.Join(*outDir, name))
if err != nil {
log.Fatal(err)
}
kw := bufio.NewWriter(kf)
writeKeys(kw, sw, set, *keys, *keySeed, full)
if full {
headers := "headers.txt"
if set != "" {
headers = "headers-" + set + ".txt"
}
write(filepath.Join(*outDir, headers), strings.Join(fullFeatureHeaders(setPrefix(set)), "\n")+"\n")
}
kw.Flush()
kf.Close()
}
sw.Flush()
sf.Close()
log.Printf("wrote %d deployments in %d groups, %d keys in each of %d sets (%s features) to %s",
n, *chatGroups+*messagesGroups+*decisionsGroups, *keys, len(sets), *features, *outDir)
}
func write(path, content string) {
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
log.Fatal(err)
}
}