feat: expose cost and reasoning levels in /v1/models (#65)

Discovery clients only saw reasoning: true, so harnesses such as OpenClaw
offered just off and max. Reasoning models now list the levels the gateway
forwards in reasoning_efforts and compat.supportedReasoningEfforts, capped
at the probed ceiling for auto, and report a forced or probed level as
default_reasoning_effort. Models with published pricing carry a cost
object, including cache read and write prices now decoded from models.dev.

Closes #65.
This commit is contained in:
Jason Xu committed 2026-10-15 08:14:11 +08:00
1 parent d04a9c750d
commit d434794866
6 files changed
+67

No files matched your search

+46
View File
@@ -2,6 +2,7 @@ package gateway
import (
"net/http"
"slices"
"time"
"opencode2api/internal/buildinfo"
@@ -118,6 +119,22 @@ func (g *Gateway) availableModels() ([]modelcatalog.Route, modelcatalog.CatalogS
return g.catalog.AvailableModels(g.zenNodes.Len() > 0, g.goNodes.Len() > 0, g.cfg.Anonymous)
}
// reasoningLevels are the effort values the gateway accepts and forwards, from
// lowest to highest. "auto" and "none" are gateway controls, not levels.
var reasoningLevels = []string{"minimal", "low", "medium", "high", "xhigh", "max"}
// reasoningEfforts lists the levels a reasoning model can be asked for. When an
// "auto" probe has found the highest level the upstream accepts, the list stops
// there so clients do not offer levels that would be rejected.
func (g *Gateway) reasoningEfforts(model string, route modelcatalog.Route) []string {
if g.cfg.ForcedEffort(model) == "auto" {
if ceiling := slices.Index(reasoningLevels, g.resolvedEffort(model, route.Tier, route.Protocol)); ceiling >= 0 {
return slices.Clone(reasoningLevels[:ceiling+1])
}
}
return slices.Clone(reasoningLevels)
}
func (g *Gateway) handleModels(w http.ResponseWriter, _ *http.Request) {
now := time.Now().Unix()
routes, _ := g.availableModels()
@@ -152,6 +169,35 @@ func (g *Gateway) handleModels(w http.ResponseWriter, _ *http.Request) {
if md.Reasoning {
entry["reasoning"] = true
entry["supports_reasoning"] = true
efforts := g.reasoningEfforts(model, route)
entry["reasoning_efforts"] = efforts
// OpenClaw and similar harnesses read effort levels from compat
// rather than inferring them, and otherwise offer only off/max.
entry["compat"] = map[string]any{"supportsReasoningEffort": true, "supportedReasoningEfforts": efforts}
if effort := g.resolvedEffort(model, route.Tier, route.Protocol); effort != "" && effort != "none" {
entry["default_reasoning_effort"] = effort
}
}
if md.Cost != nil {
cost := map[string]any{}
// Camel-case aliases match OpenClaw's model cost shape.
for _, field := range []struct {
names []string
value *float64
}{
{[]string{"input"}, md.Cost.Input},
{[]string{"output"}, md.Cost.Output},
{[]string{"cache_read", "cacheRead"}, md.Cost.CacheRead},
{[]string{"cache_write", "cacheWrite"}, md.Cost.CacheWrite},
} {
if field.value == nil {
continue
}
for _, name := range field.names {
cost[name] = *field.value
}
}
entry["cost"] = cost
}
if md.ToolCall {
entry["tool_call"] = true
+3
View File
@@ -515,6 +515,9 @@ func (c *Catalog) MetadataForTier(model string, tier config.Tier) Metadata {
if md.MaxOutput == 0 {
md.MaxOutput = price.MaxOutput
}
if price.Input != nil || price.Output != nil {
md.Cost = &Cost{Input: price.Input, Output: price.Output, CacheRead: price.CacheRead, CacheWrite: price.CacheWrite}
}
return md
}
+11
View File
@@ -73,6 +73,17 @@ type Metadata struct {
StructuredOutput bool `json:"structured_output,omitempty"`
InputModalities []string `json:"input_modalities,omitempty"`
OutputModalities []string `json:"output_modalities,omitempty"`
// Cost is filled from models.dev at read time, in USD per million tokens.
Cost *Cost `json:"cost,omitempty"`
}
// Cost is the published per-million-token price of a model. A nil field means
// the metadata did not state it.
type Cost struct {
Input *float64 `json:"input,omitempty"`
Output *float64 `json:"output,omitempty"`
CacheRead *float64 `json:"cache_read,omitempty"`
CacheWrite *float64 `json:"cache_write,omitempty"`
}
func (m *capabilityModel) metadata() Metadata {
+3
View File
@@ -30,6 +30,8 @@ type Price struct {
ID string `json:"id"`
Input *float64 `json:"input_cost,omitempty"`
Output *float64 `json:"output_cost,omitempty"`
CacheRead *float64 `json:"cache_read_cost,omitempty"`
CacheWrite *float64 `json:"cache_write_cost,omitempty"`
ContextWindow int `json:"context_window,omitempty"`
MaxInput int `json:"max_input,omitempty"`
MaxOutput int `json:"max_output,omitempty"`
@@ -326,6 +328,7 @@ func decodeModelsDev(data []byte) (map[string]Price, error) {
limit := jsonutil.MapAt(model, "limit")
result[modelID] = Price{
ID: modelID, Input: numberPointer(cost, "input"), Output: numberPointer(cost, "output"),
CacheRead: numberPointer(cost, "cache_read"), CacheWrite: numberPointer(cost, "cache_write"),
ContextWindow: jsonutil.IntAt(limit, "context"), MaxInput: jsonutil.IntAt(limit, "input"), MaxOutput: jsonutil.IntAt(limit, "output"),
Deprecated: metadataDeprecated(model),
}