Skip to content

Git designed for AI agents.

graft-resolve: pin OpenRouter routing, price only the allowed endpoints

Federico Elia76ad22b6a36e
diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.md
index db31882..fb1ba23 100644
--- a/doc/graft/RESOLVE.md
+++ b/doc/graft/RESOLVE.md
@@ -540,6 +540,7 @@ error at startup. None is dropped silently:
| `max_tokens` | `max_tokens` | `max_tokens` (default 16000) | `GRAFT_LLM_MAX_TOKENS` |
| `effort` | OpenRouter's `reasoning: {"effort": ...}`; `minimal`, `low`, `medium`, `high`, `xhigh` | `output_config.effort`; `low` to `max` (default `high`) | `GRAFT_LLM_EFFORT`, verbatim |
| `options` | error | error | error |
+| `routing` | OpenRouter's `provider` object, with `allow_fallbacks: false` | error | error |
The `openai` provider's `reasoning` parameter is OpenRouter's. Set `effort`
only for an endpoint that takes it.
@@ -565,6 +566,38 @@ Turn AI on only if you accept that the resolution is produced by a model.
Every AI resolution carries the `Graft-Resolved-By` trailer, so it can be
audited with `graft log` and reverted with `graft-verify` if it breaks tests.
+### Pinning OpenRouter routing
+
+OpenRouter serves a model from several endpoints, at different prices: for
+example, a standard tier, a flex tier and priority tiers that cost up to six
+times more. Without routing, a call can go to any of them. The
+`[strategy.llm.routing]` table of the `openai` provider pins the endpoints a
+call may use:
+
+```toml
+[strategy.llm.routing]
+only = ["openai", "azure", "amazon-bedrock"] # providers or endpoint tags allowed
+ignore = ["openai/flex", "openai/fast", "openai/ultrafast"] # endpoint tags excluded
+```
+
+The names are OpenRouter's provider slugs, or endpoint tags as the `tag`
+field of `/api/v1/models/<model>/endpoints` lists them. The provider sends
+them as the request's `provider` object, always with `allow_fallbacks:
+false`: a call that the allowed endpoints cannot serve fails rather than going
+to another endpoint. At startup the provider refuses a routing table that names
+nothing, a malformed name, or a name listed twice or in both lists.
+
+The budget is exact only when routing is pinned. Without it, the prices of the
+budget must be those of the most expensive endpoint, or a call routed there is
+undercounted. `graft-resolve bench run --pricing openrouter` prices each call
+at the highest price of the endpoints the routing allows, read at run time. It
+reads the names in the way that can only overstate the price: a slug in
+`only` admits every endpoint of that provider, its tagged variants included,
+while a name in `ignore` excludes only the endpoint whose tag is exactly that
+name. It refuses to start when the allowed set cannot be determined: when an
+endpoint has no tag, when a name matches no endpoint of the model (most likely
+a typo), or when the routing allows none.
+
### Monthly AI spending cap
An `llm` strategy can carry a monthly cap:
@@ -601,7 +634,8 @@ rewrites the file atomically (temporary file, fsync, rename), so the spend
survives restarts and is shared by every graft-resolve process that uses the
same file. The state file needs a platform with `flock` (Linux, macOS, the
BSDs). The prices are configuration values: graft-resolve does not look them
-up, and they should be set from the provider's price page.
+up, and they should be set from the provider's price page: with OpenRouter, the
+highest price of the endpoints the routing allows (see above).
The spend is reported in three places:
@@ -644,7 +678,7 @@ resolved in real repositories. It needs the grammars (a cgo build).
model needs `--max-usd`: every call books its worst case first, and a call
that would pass the cap is not made. The model's price is read at run time
from OpenRouter's endpoints API (`--pricing openrouter`, the highest price
- of the model's endpoints), or given as flags (`--pricing fixed`). The
+ of the model's endpoints, or of those the routing allows), or given as flags (`--pricing fixed`). The
answers are kept in `--answers-dir`.
- `bench rejudge --report-in R --manifest M --clones DIR --answers-dir A`:
judges the stored answers again with the current judge, with no model call.
diff --git a/tools/graft-agents/cmd/graft-resolve/bench.go b/tools/graft-agents/cmd/graft-resolve/bench.go
index 19cf6db..1523b3a 100644
--- a/tools/graft-agents/cmd/graft-resolve/bench.go
+++ b/tools/graft-agents/cmd/graft-resolve/bench.go
@@ -11,11 +11,13 @@ import (
"os"
"os/signal"
"path/filepath"
+ "reflect"
"sort"
"strings"
"syscall"
"time"
+ "github.com/feder-cr/git-ai/tools/graft-agents/llm"
"github.com/feder-cr/git-ai/tools/graft-agents/resolve"
"github.com/feder-cr/git-ai/tools/graft-agents/resolve/bench"
"github.com/feder-cr/git-ai/tools/graft-agents/resolve/syntax"
@@ -189,7 +191,17 @@ func benchRun(ctx context.Context, args []string, stdout, stderr io.Writer) int
for m := range distinct {
model = m
}
- p, err := bench.OpenRouterPricing(ctx, &http.Client{Timeout: 30 * time.Second}, *openRouterAPI, model, res.LLMMaxPromptTokens())
+ // The strategies of the one model must route it the same way, or no one price fits.
+ var routing *llm.Routing
+ first := true
+ for _, r := range res.LLMRoutings() {
+ if !first && !reflect.DeepEqual(r, routing) {
+ fmt.Fprintln(stderr, "graft-resolve bench run: --pricing openrouter needs one routing, the llm strategies route differently")
+ return 2
+ }
+ routing, first = r, false
+ }
+ p, err := bench.OpenRouterPricing(ctx, &http.Client{Timeout: 30 * time.Second}, *openRouterAPI, model, res.LLMMaxPromptTokens(), routing)
if err != nil {
fmt.Fprintf(stderr, "graft-resolve bench run: %v\n", err)
return 1
diff --git a/tools/graft-agents/llm/anthropic/anthropic.go b/tools/graft-agents/llm/anthropic/anthropic.go
index 473a41a..e23ce7a 100644
--- a/tools/graft-agents/llm/anthropic/anthropic.go
+++ b/tools/graft-agents/llm/anthropic/anthropic.go
@@ -84,6 +84,9 @@ func New(cfg llm.Config) (*Provider, error) {
if len(cfg.Command) > 0 {
return nil, errors.New("anthropic: command is for the command provider; this provider takes none")
}
+ if cfg.Routing != nil {
+ return nil, errors.New("anthropic: routing is OpenRouter's, for the openai provider; this provider takes none")
+ }
if len(cfg.Options) > 0 {
keys := make([]string, 0, len(cfg.Options))
for k := range cfg.Options {
diff --git a/tools/graft-agents/llm/anthropic/settings_test.go b/tools/graft-agents/llm/anthropic/settings_test.go
index a0f87b8..2135a32 100644
--- a/tools/graft-agents/llm/anthropic/settings_test.go
+++ b/tools/graft-agents/llm/anthropic/settings_test.go
@@ -16,6 +16,7 @@ func TestNewRejectsSettingsItCannotTake(t *testing.T) {
want string
}{
"command": {llm.Config{Type: "anthropic", Command: []string{"claude"}}, "command is for the command provider"},
+ "routing": {llm.Config{Type: "anthropic", Routing: &llm.Routing{Only: []string{"openai"}}}, "routing is OpenRouter's"},
"options": {llm.Config{Type: "anthropic", Options: map[string]string{"z": "1", "a": "2"}}, "unsupported options [a z]"},
} {
_, err := New(tc.cfg)
diff --git a/tools/graft-agents/llm/command/command.go b/tools/graft-agents/llm/command/command.go
index 2755d60..535b2a3 100644
--- a/tools/graft-agents/llm/command/command.go
+++ b/tools/graft-agents/llm/command/command.go
@@ -124,6 +124,9 @@ func New(cfg llm.Config) (*Provider, error) {
sort.Strings(keys)
unsupported = append(unsupported, fmt.Sprintf("options %v", keys))
}
+ if cfg.Routing != nil {
+ unsupported = append(unsupported, "routing")
+ }
if len(unsupported) > 0 {
return nil, fmt.Errorf("command: unsupported settings %s: the program reads its endpoint and credentials from its own environment", strings.Join(unsupported, ", "))
}
diff --git a/tools/graft-agents/llm/command/settings_test.go b/tools/graft-agents/llm/command/settings_test.go
index 2498079..c990950 100644
--- a/tools/graft-agents/llm/command/settings_test.go
+++ b/tools/graft-agents/llm/command/settings_test.go
@@ -63,6 +63,7 @@ func TestNewRejectsSettingsTheProgramCannotTake(t *testing.T) {
want string
}{
"base_url": {func(c *llm.Config) { c.BaseURL = "https://example.com/v1" }, "base_url"},
+ "routing": {func(c *llm.Config) { c.Routing = &llm.Routing{Only: []string{"openai"}} }, "routing"},
"api_key_env": {func(c *llm.Config) { c.APIKeyEnv = "KEY" }, "api_key_env"},
"options": {func(c *llm.Config) { c.Options = map[string]string{"b": "1", "a": "2"} }, "options [a b]"},
"nul effort": {func(c *llm.Config) { c.Effort = "lo\x00w" }, "effort contains a NUL byte"},
diff --git a/tools/graft-agents/llm/llm.go b/tools/graft-agents/llm/llm.go
index 1657582..f7fd0f4 100644
--- a/tools/graft-agents/llm/llm.go
+++ b/tools/graft-agents/llm/llm.go
@@ -100,6 +100,8 @@ type Config struct {
Effort string `toml:"effort" json:"effort"`
// Options holds extra provider-specific settings.
Options map[string]string `toml:"options" json:"options"`
+ // Routing pins the OpenRouter endpoints a call may use (openai provider only).
+ Routing *Routing `toml:"routing" json:"routing"`
}
// DefaultTimeout is the default completion timeout.
diff --git a/tools/graft-agents/llm/openai/openai.go b/tools/graft-agents/llm/openai/openai.go
index 4604209..95c597f 100644
--- a/tools/graft-agents/llm/openai/openai.go
+++ b/tools/graft-agents/llm/openai/openai.go
@@ -22,6 +22,7 @@ import (
"net/url"
"os"
"regexp"
+ "slices"
"sort"
"strconv"
"strings"
@@ -91,6 +92,7 @@ type Provider struct {
model string
maxTokens int
effort string
+ routing *providerPreference
timeout time.Duration
client *http.Client
maxResponseBytes int64
@@ -132,6 +134,17 @@ func New(cfg llm.Config) (*Provider, error) {
if err != nil {
return nil, err
}
+ var routing *providerPreference
+ if cfg.Routing != nil {
+ if err := cfg.Routing.Validate(); err != nil {
+ return nil, fmt.Errorf("openai: %w", err)
+ }
+ routing = &providerPreference{
+ Only: slices.Clone(cfg.Routing.Only),
+ Ignore: slices.Clone(cfg.Routing.Ignore),
+ AllowFallbacks: false,
+ }
+ }
endpoint, err := chatEndpoint(cfg.BaseURL)
if err != nil {
@@ -152,6 +165,7 @@ func New(cfg llm.Config) (*Provider, error) {
model: model,
maxTokens: cfg.MaxTokens,
effort: effort,
+ routing: routing,
timeout: timeout,
client: &http.Client{},
maxResponseBytes: defaultMaxResponseBytes,
@@ -301,6 +315,17 @@ type chatRequest struct {
MaxTokens int `json:"max_tokens,omitempty"`
ResponseFormat any `json:"response_format,omitempty"`
Reasoning *reasoning `json:"reasoning,omitempty"`
+ // Provider is OpenRouter's provider-routing object, sent when routing is configured.
+ Provider *providerPreference `json:"provider,omitempty"`
+}
+
+// providerPreference is OpenRouter's provider-routing object (llm.Routing). allow_fallbacks is
+// always sent, and always false: a call that the allowed endpoints cannot serve fails rather than
+// going to another endpoint, whose price the budget may not have counted.
+type providerPreference struct {
+ Only []string `json:"only,omitempty"`
+ Ignore []string `json:"ignore,omitempty"`
+ AllowFallbacks bool `json:"allow_fallbacks"`
}
// reasoning is OpenRouter's reasoning parameter, sent when an effort is
@@ -320,6 +345,7 @@ func (p *Provider) body(system, prompt string, maxTokens int, format any) []byte
Model: p.model,
MaxTokens: maxTokens,
ResponseFormat: format,
+ Provider: p.routing,
}
if p.effort != "" {
req.Reasoning = &reasoning{Effort: p.effort}
diff --git a/tools/graft-agents/llm/openai/routing_test.go b/tools/graft-agents/llm/openai/routing_test.go
new file mode 100644
index 0000000..bb4a7a8
--- /dev/null
+++ b/tools/graft-agents/llm/openai/routing_test.go
@@ -0,0 +1,58 @@
+package openai
+
+import (
+ "context"
+ "reflect"
+ "strings"
+ "testing"
+
+ "github.com/feder-cr/git-ai/tools/graft-agents/llm"
+)
+
+func TestRoutingSentAsProvider(t *testing.T) {
+ f := newFakeServer(t, scripted(chatOK("ok")))
+ p := newProvider(t, f, func(c *llm.Config) {
+ c.Routing = &llm.Routing{Only: []string{"openai", "azure/eu"}, Ignore: []string{"openai/ultrafast"}}
+ })
+ if _, err := p.Complete(context.Background(), llm.Request{Prompt: "hi"}); err != nil {
+ t.Fatal(err)
+ }
+ want := map[string]any{
+ "only": []any{"openai", "azure/eu"},
+ "ignore": []any{"openai/ultrafast"},
+ "allow_fallbacks": false,
+ }
+ if got := f.requests()[0].body["provider"]; !reflect.DeepEqual(got, want) {
+ t.Fatalf("provider = %v, want %v", got, want)
+ }
+}
+
+func TestNoRoutingSendsNoProvider(t *testing.T) {
+ f := newFakeServer(t, scripted(chatOK("ok")))
+ p := newProvider(t, f)
+ if _, err := p.Complete(context.Background(), llm.Request{Prompt: "hi"}); err != nil {
+ t.Fatal(err)
+ }
+ if _, ok := f.requests()[0].body["provider"]; ok {
+ t.Fatalf("provider sent without routing: %v", f.requests()[0].body)
+ }
+}
+
+func TestNewRejectsInvalidRouting(t *testing.T) {
+ for name, tc := range map[string]struct {
+ routing llm.Routing
+ want string
+ }{
+ "empty": {llm.Routing{}, "names no provider"},
+ "bad name": {llm.Routing{Only: []string{"Open AI"}}, `"Open AI" is not a provider slug`},
+ "bad variant": {llm.Routing{Ignore: []string{"openai/"}}, `"openai/" is not a provider slug`},
+ "both": {llm.Routing{Only: []string{"openai"}, Ignore: []string{"openai"}}, "listed in both only and ignore"},
+ "twice": {llm.Routing{Only: []string{"azure", "azure"}}, "listed twice"},
+ } {
+ r := tc.routing
+ _, err := New(llm.Config{Type: "openai", Model: "m", Routing: &r})
+ if err == nil || !strings.Contains(err.Error(), tc.want) {
+ t.Errorf("%s: err = %v, want %q", name, err, tc.want)
+ }
+ }
+}
diff --git a/tools/graft-agents/llm/routing.go b/tools/graft-agents/llm/routing.go
new file mode 100644
index 0000000..cc9efe2
--- /dev/null
+++ b/tools/graft-agents/llm/routing.go
@@ -0,0 +1,92 @@
+package llm
+
+import (
+ "fmt"
+ "regexp"
+ "slices"
+ "strings"
+)
+
+// Routing is an OpenRouter provider-routing preference: the endpoints a call may be served by.
+// The openai provider sends it as the request's "provider" object, with allow_fallbacks false so
+// that a call can never escape the allowed set; every other provider rejects it.
+//
+// A name is a provider slug ("openai", "azure") or an endpoint tag ("openai/flex", "azure/eu"),
+// as the tag field of OpenRouter's /models/<model>/endpoints lists them. Pricing a call from the
+// endpoints (Allows) reads the names the way that can only overstate the cost: a slug in Only
+// admits every endpoint of that provider, its tagged variants (such as a priority tier) included,
+// while a name in Ignore excludes only the endpoint whose tag is exactly that name. So the budget
+// is exact only when routing is pinned to the endpoints the run means to use, and it is never below
+// the price of an endpoint the call can reach.
+type Routing struct {
+ // Only lists the providers or endpoints a call may use; empty allows every one not ignored.
+ Only []string `toml:"only" json:"only"`
+ // Ignore lists the providers or endpoints a call must not use.
+ Ignore []string `toml:"ignore" json:"ignore"`
+}
+
+// routingNameRE is the form of an OpenRouter provider slug, optionally with an endpoint variant.
+var routingNameRE = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*(/[a-z0-9][a-z0-9.-]*)?$`)
+
+// Validate checks that r names at least one provider or endpoint, each well-formed, once, and none
+// both allowed and ignored. Whether a name is one of the model's endpoints is known only from
+// OpenRouter's endpoint list (CheckNames).
+func (r *Routing) Validate() error {
+ if len(r.Only) == 0 && len(r.Ignore) == 0 {
+ return fmt.Errorf("llm: routing names no provider: set only, ignore or both")
+ }
+ seen := map[string]string{}
+ for _, list := range []struct {
+ key string
+ names []string
+ }{{"only", r.Only}, {"ignore", r.Ignore}} {
+ for _, name := range list.names {
+ if !routingNameRE.MatchString(name) {
+ return fmt.Errorf("llm: routing %s: %q is not a provider slug or endpoint tag (such as openai or azure/eu)", list.key, name)
+ }
+ if prev, dup := seen[name]; dup {
+ if prev == list.key {
+ return fmt.Errorf("llm: routing %s: %q is listed twice", list.key, name)
+ }
+ return fmt.Errorf("llm: routing: %q is listed in both %s and %s", name, prev, list.key)
+ }
+ seen[name] = list.key
+ }
+ }
+ return nil
+}
+
+// Allows reports whether a call routed by r may be served by the endpoint with tag, read the way
+// that prices the call no lower than any endpoint it can reach (see Routing).
+func (r *Routing) Allows(tag string) bool {
+ if r == nil {
+ return true
+ }
+ if slices.Contains(r.Ignore, tag) {
+ return false
+ }
+ if len(r.Only) == 0 {
+ return true
+ }
+ return slices.ContainsFunc(r.Only, func(name string) bool { return routingNameMatches(name, tag) })
+}
+
+// CheckNames checks every name of r against the tags of the model's endpoints: a name that matches
+// none is unknown, most likely a typo, and an error, as is a preference that allows no endpoint.
+func (r *Routing) CheckNames(tags []string) error {
+ for _, name := range slices.Concat(r.Only, r.Ignore) {
+ if !slices.ContainsFunc(tags, func(tag string) bool { return routingNameMatches(name, tag) }) {
+ return fmt.Errorf("llm: routing names %q, which is no endpoint of the model (endpoints: %v)", name, tags)
+ }
+ }
+ if !slices.ContainsFunc(tags, r.Allows) {
+ return fmt.Errorf("llm: routing allows none of the model's endpoints %v", tags)
+ }
+ return nil
+}
+
+// routingNameMatches reports whether name designates the endpoint with tag: the tag itself, or,
+// for a slug without a variant, any variant of that provider.
+func routingNameMatches(name, tag string) bool {
+ return name == tag || (!strings.Contains(name, "/") && strings.HasPrefix(tag, name+"/"))
+}
diff --git a/tools/graft-agents/resolve/bench/pricing.go b/tools/graft-agents/resolve/bench/pricing.go
index 7b30b5d..b4edd13 100644
--- a/tools/graft-agents/resolve/bench/pricing.go
+++ b/tools/graft-agents/resolve/bench/pricing.go
@@ -14,6 +14,7 @@ import (
"strings"
"time"
+ "github.com/feder-cr/git-ai/tools/graft-agents/llm"
"github.com/feder-cr/git-ai/tools/graft-agents/resolve"
)
@@ -21,13 +22,17 @@ import (
const DefaultOpenRouterAPI = "https://openrouter.ai/api/v1"
// Pricing is the price of the measured model, as read at run time (never from memory, PLAN10 12):
-// the highest price of each kind over the model's endpoints, in USD per token, since OpenRouter
-// may route a call to any of them.
+// the highest price of each kind over the endpoints a call may reach, in USD per token, since
+// OpenRouter may route a call to any of them. Without routing that is every endpoint of the model;
+// with routing (llm.Routing) it is the endpoints the routing allows.
type Pricing struct {
Model string `json:"model"`
Source string `json:"source"`
FetchedAt time.Time `json:"fetched_at"`
- Endpoints int `json:"endpoints"`
+ // Endpoints is the number of endpoints of the model, and Priced the tags of those the prices
+ // come from when routing narrows them (empty: every endpoint).
+ Endpoints int `json:"endpoints"`
+ Priced []string `json:"priced_endpoints,omitempty"`
// Prompt, Completion and Reasoning are per token; Request is per call.
Prompt float64 `json:"prompt_usd_per_token"`
Completion float64 `json:"completion_usd_per_token"`
@@ -67,6 +72,7 @@ type openRouterEndpoints struct {
ID string `json:"id"`
Endpoints []struct {
Name string `json:"name"`
+ Tag string `json:"tag"`
Pricing map[string]json.RawMessage `json:"pricing"`
} `json:"endpoints"`
} `json:"data"`
@@ -92,7 +98,11 @@ func openRouterPrice(raw json.RawMessage) (float64, error) {
//
// maxPromptTokens is an upper bound of the prompt of one call; an override tier whose
// min_prompt_tokens is above it is left out, since it cannot apply. 0 counts every tier.
-func OpenRouterPricing(ctx context.Context, client *http.Client, api, model string, maxPromptTokens int) (Pricing, error) {
+//
+// routing, when set, narrows the endpoints to those it allows (llm.Routing.Allows). The allowed set
+// must be known exactly, so the pricing fails when an endpoint has no tag, when a name of routing
+// matches no endpoint, or when routing allows none.
+func OpenRouterPricing(ctx context.Context, client *http.Client, api, model string, maxPromptTokens int, routing *llm.Routing) (Pricing, error) {
parts := strings.Split(model, "/")
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return Pricing{}, fmt.Errorf("pricing: model %q is not author/slug", model)
@@ -127,6 +137,18 @@ func OpenRouterPricing(ctx context.Context, client *http.Client, api, model stri
// towards the highest price too. A discount is ignored, which can only overstate the cost, and so are the prices of
// what the run never uses (web search, images, cache reads and writes).
p := Pricing{Model: model, Source: source, FetchedAt: time.Now().UTC(), Endpoints: len(doc.Data.Endpoints)}
+ if routing != nil {
+ tags := make([]string, 0, len(doc.Data.Endpoints))
+ for _, e := range doc.Data.Endpoints {
+ if e.Tag == "" {
+ return Pricing{}, fmt.Errorf("pricing: endpoint %q of %s has no tag, so the endpoints the routing allows are not known", e.Name, model)
+ }
+ tags = append(tags, e.Tag)
+ }
+ if err := routing.CheckNames(tags); err != nil {
+ return Pricing{}, fmt.Errorf("pricing: %w", err)
+ }
+ }
take := func(endpoint string, prices map[string]json.RawMessage, required bool) error {
for _, f := range []struct {
key string
@@ -154,6 +176,12 @@ func OpenRouterPricing(ctx context.Context, client *http.Client, api, model stri
return nil
}
for _, e := range doc.Data.Endpoints {
+ if routing != nil {
+ if !routing.Allows(e.Tag) {
+ continue
+ }
+ p.Priced = append(p.Priced, e.Tag)
+ }
if err := take(e.Name, e.Pricing, true); err != nil {
return Pricing{}, err
}
diff --git a/tools/graft-agents/resolve/bench/pricing_test.go b/tools/graft-agents/resolve/bench/pricing_test.go
index 55612e4..197b445 100644
--- a/tools/graft-agents/resolve/bench/pricing_test.go
+++ b/tools/graft-agents/resolve/bench/pricing_test.go
@@ -6,6 +6,8 @@ import (
"net/http/httptest"
"strings"
"testing"
+
+ "github.com/feder-cr/git-ai/tools/graft-agents/llm"
)
func TestOpenRouterPricing(t *testing.T) {
@@ -24,6 +26,13 @@ func TestOpenRouterPricing(t *testing.T) {
{"name":"p1","pricing":{"prompt":"0.0000001","completion":"0.0000005","web_search":"0.01","input_cache_read":"0.00000001","discount":0,
"overrides":[{"min_prompt_tokens":272000,"prompt":"0.000003","completion":"0.000004"}]}},
{"name":"p2","pricing":{"prompt":"0.0000002","completion":0.000001,"discount":0.5}}]}}`,
+ // The tiers of a model on OpenRouter (2026-10): a provider's endpoints with tagged variants.
+ "/models/a/routed/endpoints": `{"data":{"endpoints":[
+ {"name":"o","tag":"openai","pricing":{"prompt":"0.000002","completion":"0.00001"}},
+ {"name":"of","tag":"openai/fast","pricing":{"prompt":"0.000004","completion":"0.00002"}},
+ {"name":"ou","tag":"openai/ultrafast","pricing":{"prompt":"0.000012","completion":"0.00006"}},
+ {"name":"az","tag":"azure","pricing":{"prompt":"0.000003","completion":"0.00003"}},
+ {"name":"ae","tag":"azure/eu","pricing":{"prompt":"0.0000022","completion":"0.000011"}}]}}`,
"/models/a/badtier/endpoints": `{"data":{"endpoints":[{"name":"x","pricing":{"prompt":"0.1","completion":"0.1","overrides":[{"prompt":"-3"}]}}]}}`,
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -36,7 +45,7 @@ func TestOpenRouterPricing(t *testing.T) {
}))
defer srv.Close()
- p, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL+"/", "a/ok", 0)
+ p, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL+"/", "a/ok", 0, nil)
if err != nil {
t.Fatal(err)
}
@@ -51,7 +60,7 @@ func TestOpenRouterPricing(t *testing.T) {
// Prompts of at most 100k tokens never reach p1's tier (from 272k tokens): the highest prices
// are p2's, prompt 2e-7 and completion 1e-6 (a number); the discount and the unused prices
// change nothing.
- real, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL, "a/real", 100_000)
+ real, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL, "a/real", 100_000, nil)
if err != nil {
t.Fatal(err)
}
@@ -63,7 +72,7 @@ func TestOpenRouterPricing(t *testing.T) {
t.Fatalf("prices %+v", got)
}
// Without a bound every tier counts.
- all, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL, "a/real", 0)
+ all, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL, "a/real", 0, nil)
if err != nil {
t.Fatal(err)
}
@@ -71,6 +80,38 @@ func TestOpenRouterPricing(t *testing.T) {
t.Fatalf("pricing with every tier %+v", all)
}
+ // Routing prices only the allowed endpoints. "openai" admits its variants (a priority tier
+ // included) and "ignore" excludes exactly the tag it names.
+ only := &llm.Routing{Only: []string{"openai", "azure/eu"}, Ignore: []string{"openai/ultrafast"}}
+ routed, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL, "a/routed", 0, only)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if routed.Prompt != 0.000004 || routed.Completion != 0.00002 || strings.Join(routed.Priced, ",") != "openai,openai/fast,azure/eu" {
+ t.Fatalf("routed pricing %+v, want the highest of openai, openai/fast and azure/eu", routed)
+ }
+ unrouted, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL, "a/routed", 0, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if unrouted.Completion != 0.00006 || len(unrouted.Priced) != 0 {
+ t.Fatalf("unrouted pricing %+v, want the highest of every endpoint", unrouted)
+ }
+ for name, tc := range map[string]struct {
+ model string
+ routing *llm.Routing
+ want string
+ }{
+ "unknown name": {"a/routed", &llm.Routing{Only: []string{"openia"}}, `routing names "openia"`},
+ "unknown ignore": {"a/routed", &llm.Routing{Ignore: []string{"openai/turbo"}}, `routing names "openai/turbo"`},
+ "nothing allowed": {"a/routed", &llm.Routing{Only: []string{"azure/eu"}, Ignore: []string{"azure/eu"}}, "allows none"},
+ "no tag": {"a/ok", &llm.Routing{Only: []string{"openai"}}, "has no tag"},
+ } {
+ if _, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL, tc.model, 0, tc.routing); err == nil || !strings.Contains(err.Error(), tc.want) {
+ t.Errorf("%s: err = %v, want %q", name, err, tc.want)
+ }
+ }
+
for model, want := range map[string]string{
"a/badtier": "(override)\" of a/badtier has the prompt price",
"a/none": "no endpoint",
@@ -81,7 +122,7 @@ func TestOpenRouterPricing(t *testing.T) {
"a/missing": "HTTP 404",
"no-slash": "not author/slug",
} {
- if _, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL, model, 0); err == nil || !strings.Contains(err.Error(), want) {
+ if _, err := OpenRouterPricing(context.Background(), srv.Client(), srv.URL, model, 0, nil); err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("%s: err = %v, want %q", model, err, want)
}
}
diff --git a/tools/graft-agents/resolve/bench/report.go b/tools/graft-agents/resolve/bench/report.go
index 6a2aa52..b48c6ba 100644
--- a/tools/graft-agents/resolve/bench/report.go
+++ b/tools/graft-agents/resolve/bench/report.go
@@ -126,8 +126,12 @@ func (rep Report) WriteMarkdown(w io.Writer) error {
}
b.WriteString(".\n\n")
if p := rep.Pricing; p != nil {
- fmt.Fprintf(&b, "Model %s, priced at run time (%s, %s, highest of %d endpoints): $%g per million input tokens, $%g per million output tokens (completion $%g + internal reasoning $%g), $%g per request.\n\n",
- p.Model, p.Source, p.FetchedAt.Format(time.RFC3339), p.Endpoints,
+ over := fmt.Sprintf("highest of %d endpoints", p.Endpoints)
+ if len(p.Priced) > 0 {
+ over = fmt.Sprintf("highest of the %d of %d endpoints the routing allows: %s", len(p.Priced), p.Endpoints, strings.Join(p.Priced, ", "))
+ }
+ fmt.Fprintf(&b, "Model %s, priced at run time (%s, %s, %s): $%g per million input tokens, $%g per million output tokens (completion $%g + internal reasoning $%g), $%g per request.\n\n",
+ p.Model, p.Source, p.FetchedAt.Format(time.RFC3339), over,
PerMillion(p.Prompt), PerMillion(p.Completion, p.Reasoning), PerMillion(p.Completion), PerMillion(p.Reasoning), p.Request)
}
b.WriteString("| family | cases | correct (exact) | wrong | rejected by checks | not sent | model error | not run (cap) | abstained | error rate |\n")
diff --git a/tools/graft-agents/resolve/budget_strategy.go b/tools/graft-agents/resolve/budget_strategy.go
index 88e08c2..0fb8a9c 100644
--- a/tools/graft-agents/resolve/budget_strategy.go
+++ b/tools/graft-agents/resolve/budget_strategy.go
@@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"sort"
+
+ "github.com/feder-cr/git-ai/tools/graft-agents/llm"
)
// The llm strategy's budget is the [strategy.llm.budget] table:
@@ -121,6 +123,18 @@ func (r *Resolver) LLMModels() map[string]string {
return out
}
+// LLMRoutings returns the OpenRouter routing of each llm strategy of r, by strategy name; nil for
+// a strategy without one.
+func (r *Resolver) LLMRoutings() map[string]*llm.Routing {
+ out := map[string]*llm.Routing{}
+ for _, bd := range r.bindings {
+ if s, ok := bd.Strategy.(*llmStrategy); ok {
+ out[bd.Name] = s.routing
+ }
+ }
+ return out
+}
+
// LLMMaxPromptTokens returns an upper bound of the input tokens of one model call of r's llm
// strategies, counted like the budget's worst case: one token per byte. A prompt holds file data
// within max_prompt_bytes (whole file or one region), the two commit messages, the system prompt,
diff --git a/tools/graft-agents/resolve/llm_strategy.go b/tools/graft-agents/resolve/llm_strategy.go
index c9cbf6e..7d1d4e9 100644
--- a/tools/graft-agents/resolve/llm_strategy.go
+++ b/tools/graft-agents/resolve/llm_strategy.go
@@ -62,8 +62,9 @@ type llmStrategy struct {
provider llm.Provider
base llm.Provider
budget *Budget
- // model is the configured model name.
+ // model is the configured model name, and routing its OpenRouter routing, if any.
model string
+ routing *llm.Routing
opt LLMOptions
timeout time.Duration
}
@@ -118,7 +119,7 @@ func newLLMStrategy(spec StrategySpec) (Strategy, error) {
if err != nil {
return nil, fmt.Errorf("strategy %q: %w", spec.Name, err)
}
- s := &llmStrategy{provider: p, base: p, model: cfg.Model, opt: opt, timeout: timeout}
+ s := &llmStrategy{provider: p, base: p, model: cfg.Model, routing: cfg.Routing, opt: opt, timeout: timeout}
budget, err := llmBudgetConfig(spec, spec.Options["llm"].(map[string]any))
if err != nil {
@@ -193,7 +194,7 @@ func llmProviderConfig(spec StrategySpec) (llm.Config, error) {
var unknown []string
for k := range tbl {
switch k {
- case "type", "model", "base_url", "api_key_env", "command", "timeout", "max_tokens", "effort", "options", "budget":
+ case "type", "model", "base_url", "api_key_env", "command", "timeout", "max_tokens", "effort", "options", "routing", "budget":
default:
unknown = append(unknown, k)
}
@@ -240,12 +241,39 @@ func llmProviderConfig(spec StrategySpec) (llm.Config, error) {
cfg.Options[k] = s
}
}
+ if v, ok := tbl["routing"]; ok {
+ if cfg.Routing, err = routingField(v); err != nil {
+ return llm.Config{}, fmt.Errorf("strategy %q: llm: routing: %w", spec.Name, err)
+ }
+ }
if cfg.Type == "" {
return llm.Config{}, fmt.Errorf("strategy %q: llm: type is required", spec.Name)
}
return cfg, nil
}
+// routingField reads the [strategy.llm.routing] table: the lists only and ignore, and nothing else.
+func routingField(v any) (*llm.Routing, error) {
+ tbl, ok := v.(map[string]any)
+ if !ok {
+ return nil, fmt.Errorf("must be a table")
+ }
+ for k := range tbl {
+ if k != "only" && k != "ignore" {
+ return nil, fmt.Errorf("unknown key %q (want only, ignore)", k)
+ }
+ }
+ var r llm.Routing
+ var err error
+ if r.Only, err = stringListField(tbl, "only"); err != nil {
+ return nil, err
+ }
+ if r.Ignore, err = stringListField(tbl, "ignore"); err != nil {
+ return nil, err
+ }
+ return &r, nil
+}
+
// llmOptInt reads an optional non-negative integer option of the strategy.
func llmOptInt(spec StrategySpec, key string) (int, error) {
v, ok := spec.Options[key]
diff --git a/tools/graft-agents/resolve/llm_strategy_test.go b/tools/graft-agents/resolve/llm_strategy_test.go
index 0a10a10..f74e4bc 100644
--- a/tools/graft-agents/resolve/llm_strategy_test.go
+++ b/tools/graft-agents/resolve/llm_strategy_test.go
@@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"errors"
+ "reflect"
"regexp"
"strings"
"sync"
@@ -424,6 +425,18 @@ type = "llm"
config: base + "[strategy.llm]\ntype = \"wp19-resolve-fake\"\nmax_tokens = -5\n",
want: "max_tokens must be an integer",
},
+ "routing not a table": {
+ config: base + "[strategy.llm]\ntype = \"wp19-resolve-fake\"\nrouting = \"openai\"\n",
+ want: "routing: must be a table",
+ },
+ "unknown routing key": {
+ config: base + "[strategy.llm]\ntype = \"wp19-resolve-fake\"\n[strategy.llm.routing]\norder = [\"openai\"]\n",
+ want: `unknown key "order"`,
+ },
+ "routing only not a list": {
+ config: base + "[strategy.llm]\ntype = \"wp19-resolve-fake\"\n[strategy.llm.routing]\nonly = \"openai\"\n",
+ want: `"only" must be a list of strings`,
+ },
} {
t.Run(name, func(t *testing.T) {
_, err := wp19BuildErr(tc.config)
@@ -434,6 +447,27 @@ type = "llm"
}
}
+func TestLLMStrategyRouting(t *testing.T) {
+ r := wp19Build(t, `
+[[strategy]]
+type = "llm"
+[strategy.llm]
+type = "wp19-resolve-fake"
+[strategy.llm.routing]
+only = ["openai", "azure/eu"]
+ignore = ["openai/ultrafast"]
+`)
+ want := &llm.Routing{Only: []string{"openai", "azure/eu"}, Ignore: []string{"openai/ultrafast"}}
+ for name, got := range r.LLMRoutings() {
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("strategy %s: routing %+v, want %+v", name, got, want)
+ }
+ }
+ if len(r.LLMRoutings()) != 1 {
+ t.Fatalf("routings %v, want one", r.LLMRoutings())
+ }
+}
+
func TestLLMStrategyNoConfigDefaults(t *testing.T) {
// Only the table is needed; the timeout is the default.
wp19SetAnswer(func(context.Context, llm.Request) (*llm.Response, error) {