Files
coder/coderd/x/chatd/chatprovider/reasoningeffort.go
T
Ethan feea5ab959 feat: add GPT-6.2 Astra and GPT-6.2 Sol to Agents and AI Gateway (#32370)
Closes CODAGT-1608

This adds `gpt-6.2-astra` and `gpt-6.2-sol` to the Agents known models
catalog, so they're suggested when an admin adds a chat model, and
regenerates the AI Gateway price book from models.dev so the gateway can
price them.

| Model | Input | Output | Cache read | Cache write |
|---|---|---|---|---|
| GPT-6.2 Astra | $8 | $40 | $0.40 | $10 |
| GPT-6.2 Sol | $2 | $10 | $0.10 | $2.50 |

Prices are per 1M tokens, and both models have a 1,050,000 token context
window and 128,000 max output tokens. They're in the catalog for OpenAI
(with a suggested effort of `medium`, same as `gpt-6-astra` and
`gpt-6.1-sol`), OpenRouter and Vercel. models.dev doesn't list them on
Azure or Bedrock yet, so I've left those out for now.

Both models also reject the `none` and `minimal` reasoning efforts. A
model config only sets a default and a maximum effort, so the picker
always offers both, and picking either one fails the turn:

```
Unsupported value: 'none' is not supported with the 'gpt-6.2-astra' model. Supported values are: 'low', 'medium', 'high', 'xhigh', and 'max'.
```

We already send `low` instead for `gpt-6-astra` and `gpt-6.1-sol`, so to
fix, we'll just do the same for the two new models. All four now live in
one list of model ID prefixes, rather than a helper each. I checked this
against the real models: all four return a 400 like the one above for
`none` and `minimal`, and accept `low`. `gpt-6-sol` and `gpt-6.1-terra`
still accept `none`, so they're left alone.

Regenerating the price book also brings in everything else models.dev
changed since the last weekly refresh, mostly on OpenRouter and Vercel:
22 other new entries (4 of them `-pro` and `-fast` variants of these
two), 29 it dropped and 51 price changes. 6 existing catalog entries get
new prices or limits the same way.
2026-11-19 00:33:46 +11:00

372 lines
12 KiB
Go

package chatprovider
import (
"slices"
"strconv"
"strings"
"charm.land/fantasy"
fantasyanthropic "charm.land/fantasy/providers/anthropic"
fantasygoogle "charm.land/fantasy/providers/google"
fantasyopenai "charm.land/fantasy/providers/openai"
fantasyopenaicompat "charm.land/fantasy/providers/openaicompat"
"github.com/coder/coder/v2/codersdk"
)
func reasoningEffortRank(value string) (int, bool) {
rank := slices.Index(codersdk.ChatModelReasoningEffortValues(), value)
return rank, rank >= 0
}
func IsValidReasoningEffort(value string) bool {
_, ok := reasoningEffortRank(value)
return ok
}
// ReasoningEffortLessOrEqual reports whether a is lower than or equal
// to b on the global effort scale. Unknown values return false.
func ReasoningEffortLessOrEqual(a, b string) bool {
aRank, aOK := reasoningEffortRank(a)
bRank, bOK := reasoningEffortRank(b)
return aOK && bOK && aRank <= bRank
}
// ResolveReasoningEffort computes the effective reasoning effort for a
// generation. The requested per-turn value wins over the config's default,
// and the result is clamped to the config's max on the global scale. Returns
// nil when the model config has no reasoning effort configured, no usable
// value remains, or the max is unknown.
func ResolveReasoningEffort(
requested *string,
config *codersdk.ChatModelReasoningEffortConfig,
) *string {
if config == nil {
return nil
}
effective := requested
var rank int
var ok bool
if effective != nil {
rank, ok = reasoningEffortRank(*effective)
}
if !ok {
effective = config.Default
if effective != nil {
rank, ok = reasoningEffortRank(*effective)
}
}
if !ok {
return nil
}
if config.Max != nil {
maxRank, ok := reasoningEffortRank(*config.Max)
if !ok {
return nil
}
if rank > maxRank {
return config.Max
}
}
return effective
}
func SelectableReasoningEfforts(
config *codersdk.ChatModelReasoningEffortConfig,
) []string {
if config == nil || config.Max == nil {
return nil
}
maxRank, ok := reasoningEffortRank(*config.Max)
if !ok {
return nil
}
values := codersdk.ChatModelReasoningEffortValues()
return values[:maxRank+1]
}
func applyReasoningEffort(
model Model,
options fantasy.ProviderOptions,
effort *string,
) fantasy.ProviderOptions {
if effort == nil || !model.Valid() {
return options
}
if options == nil {
options = fantasy.ProviderOptions{}
}
switch NormalizeProvider(model.Provider()) {
case fantasyopenai.Name:
providerEffort := fantasyopenai.ReasoningEffort(openAIReasoningEffort(model.ModelID(), *effort))
switch opts := options[fantasyopenai.Name].(type) {
case *fantasyopenai.ResponsesProviderOptions:
opts.ReasoningEffort = &providerEffort
case *fantasyopenai.ProviderOptions:
opts.ReasoningEffort = &providerEffort
default:
if model.transport.UsesResponses() {
options[fantasyopenai.Name] = &fantasyopenai.ResponsesProviderOptions{
ReasoningEffort: &providerEffort,
}
return options
}
options[fantasyopenai.Name] = &fantasyopenai.ProviderOptions{
ReasoningEffort: &providerEffort,
}
}
case fantasyanthropic.Name:
providerEffort := fantasyanthropic.Effort(anthropicReasoningEffort(model.ModelID(), *effort))
providerOptions := ensureProviderOptions[fantasyanthropic.ProviderOptions](options, fantasyanthropic.Name)
providerOptions.Effort = &providerEffort
case fantasyopenaicompat.Name:
providerEffort := fantasyopenai.ReasoningEffort(*effort)
if compatEffort, ok := googleCompatReasoningEffort(model.ModelID(), *effort); ok {
providerEffort = fantasyopenai.ReasoningEffort(compatEffort)
}
providerOptions := ensureProviderOptions[fantasyopenaicompat.ProviderOptions](options, fantasyopenaicompat.Name)
providerOptions.ReasoningEffort = &providerEffort
}
return options
}
// openAILowFloorPrefixes lists the OpenAI models that reject none and minimal
// with HTTP 400, so they start at low. Each entry is a lowercase model ID
// prefix, which also matches the model's dated snapshots.
var openAILowFloorPrefixes = []string{
"gpt-6-astra",
"gpt-6.1-sol",
"gpt-6.2-astra",
"gpt-6.2-sol",
}
// openAIReasoningEffort maps the global reasoning effort scale onto what an
// OpenAI model accepts.
func openAIReasoningEffort(modelID, effort string) string {
id := strings.ToLower(modelID)
startsAtLow := slices.ContainsFunc(openAILowFloorPrefixes, func(prefix string) bool {
return strings.HasPrefix(id, prefix)
})
if !startsAtLow {
return effort
}
switch effort {
case codersdk.ChatModelReasoningEffortNone, codersdk.ChatModelReasoningEffortMinimal:
return codersdk.ChatModelReasoningEffortLow
}
return effort
}
// anthropicReasoningEffort maps the global reasoning effort scale onto what an
// Anthropic model accepts. Claude Opus 5.5 and Sonnet 5.5 always run adaptive
// thinking and reject the thinking: {type: "disabled"} that fantasy sends for
// none with HTTP 400, so none starts at low.
func anthropicReasoningEffort(modelID, effort string) string {
if effort != codersdk.ChatModelReasoningEffortNone {
return effort
}
id := strings.ToLower(modelID)
if strings.Contains(id, "claude-opus-5-5") || strings.Contains(id, "claude-sonnet-5-5") {
return codersdk.ChatModelReasoningEffortLow
}
return effort
}
// googleCompatReasoningEffort maps the global reasoning effort scale onto the
// reasoning_effort values a Gemini model accepts behind an OpenAI-compatible
// endpoint, reporting ok=false for non-Gemini model IDs. Google's compat
// layer translates reasoning_effort into the model's thinking configuration
// but validates instead of clamping, so out-of-range values (including the
// Coder-only xhigh and max) fail the whole request with HTTP 400.
func googleCompatReasoningEffort(modelID, effort string) (string, bool) {
normalized := strings.TrimPrefix(strings.ToLower(strings.TrimSpace(modelID)), "models/")
normalized = strings.TrimPrefix(normalized, "google/")
if !strings.HasPrefix(normalized, "gemini-") {
return "", false
}
if supported := googleSupportedThinkingLevels(normalized); len(supported) > 0 {
level := clampGoogleThinkingLevel(googleThinkingLevel(effort), supported)
return strings.ToLower(level), true
}
// Pre-Gemini-3 models translate reasoning_effort into a thinking budget
// and accept only none/low/medium/high. Pro models cannot disable
// thinking, so "none" is rejected for them and clamps up to low.
isPro := slices.Contains(strings.Split(strings.TrimPrefix(normalized, "gemini-"), "-"), "pro")
switch effort {
case codersdk.ChatModelReasoningEffortNone:
if isPro {
return codersdk.ChatModelReasoningEffortLow, true
}
return codersdk.ChatModelReasoningEffortNone, true
case codersdk.ChatModelReasoningEffortMinimal, codersdk.ChatModelReasoningEffortLow:
return codersdk.ChatModelReasoningEffortLow, true
case codersdk.ChatModelReasoningEffortMedium:
return codersdk.ChatModelReasoningEffortMedium, true
default:
return codersdk.ChatModelReasoningEffortHigh, true
}
}
// googleThinkingLevelsAscending orders Google thinking levels from least to
// most thinking, for clamping into a model's supported subset.
var googleThinkingLevelsAscending = []fantasygoogle.ThinkingLevel{
fantasygoogle.ThinkingLevelMinimal,
fantasygoogle.ThinkingLevelLow,
fantasygoogle.ThinkingLevelMedium,
fantasygoogle.ThinkingLevelHigh,
}
// googleSupportedThinkingLevels returns the thinking_level values the Google
// model accepts in ascending order, or nil when the model does not accept
// thinking_level at all. Gemini introduced thinking_level in version 3, and
// each model supports a different subset: Gemini 3 Pro launched with LOW and
// HIGH, 3.1 Pro added MEDIUM, the Flash family accepted all four through 3.6
// but 3.7 dropped MINIMAL, and image models accept only HIGH (plus MINIMAL
// for flash). Versionless "-latest" aliases track the newest release of their
// family, which has been Gemini 3+ since the aliases were introduced.
// Gemini 4 Argon accepts the same levels as 3.1 Pro (live-verified 2026-10-14).
// Only the Pro, Argon, and Flash chat families qualify; specialized variants
// such as live, TTS, and audio models reject thinking_level or generateContent
// entirely (verified live), so unrecognized name tokens fail closed and
// keep the previous request shape, matching googleSupportsThinkingBudget.
func googleSupportedThinkingLevels(modelID string) []fantasygoogle.ThinkingLevel {
normalized := strings.TrimPrefix(strings.ToLower(strings.TrimSpace(modelID)), "models/")
rest, ok := strings.CutPrefix(normalized, "gemini-")
if !ok {
return nil
}
segments := strings.Split(rest, "-")
major, minor, hasVersion := parseGoogleModelVersion(segments[0])
isLatestAlias := !hasVersion && segments[len(segments)-1] == "latest"
if hasVersion && major < 3 {
return nil
}
if !hasVersion && !isLatestAlias {
return nil
}
family := segments
if hasVersion {
family = segments[1:]
}
isPro, isFlash, isImage := false, false, false
for _, segment := range family {
switch segment {
case "pro", "argon":
isPro = true
case "flash":
isFlash = true
case "image":
isImage = true
case "lite", "preview", "exp", "latest":
default:
if _, err := strconv.Atoi(segment); err != nil {
return nil
}
}
}
if !isPro && !isFlash {
return nil
}
switch {
case isImage && isFlash:
return []fantasygoogle.ThinkingLevel{
fantasygoogle.ThinkingLevelMinimal,
fantasygoogle.ThinkingLevelHigh,
}
case isImage:
return []fantasygoogle.ThinkingLevel{fantasygoogle.ThinkingLevelHigh}
case isFlash:
// Gemini 3.7 Flash rejects MINIMAL with HTTP 400 while 3 through
// 3.6 flash and flash-lite models accept it (live-verified
// 2026-08-20). Latest aliases and unknown future versions stay
// off MINIMAL too: gemini-flash-latest already tracks 3.7 and
// rejects it, and clamping up to LOW is accepted everywhere
// while an unsupported level fails the whole request.
if isLatestAlias || major > 3 || minor >= 7 {
return []fantasygoogle.ThinkingLevel{
fantasygoogle.ThinkingLevelLow,
fantasygoogle.ThinkingLevelMedium,
fantasygoogle.ThinkingLevelHigh,
}
}
return slices.Clone(googleThinkingLevelsAscending)
case hasVersion && major == 3 && minor == 0:
return []fantasygoogle.ThinkingLevel{
fantasygoogle.ThinkingLevelLow,
fantasygoogle.ThinkingLevelHigh,
}
default:
return []fantasygoogle.ThinkingLevel{
fantasygoogle.ThinkingLevelLow,
fantasygoogle.ThinkingLevelMedium,
fantasygoogle.ThinkingLevelHigh,
}
}
}
// parseGoogleModelVersion parses a Gemini version segment such as "3" or
// "3.7" into major and minor components.
func parseGoogleModelVersion(segment string) (major, minor int, ok bool) {
majorText, minorText, hasMinor := strings.Cut(segment, ".")
major, err := strconv.Atoi(majorText)
if err != nil {
return 0, 0, false
}
if hasMinor {
minor, err = strconv.Atoi(minorText)
if err != nil {
return 0, 0, false
}
}
return major, minor, true
}
// clampGoogleThinkingLevel snaps the desired level into the model's supported
// subset: the lowest supported level at or above the desired one, so at least
// the requested reasoning depth is preserved, else the highest supported.
func clampGoogleThinkingLevel(
desired fantasygoogle.ThinkingLevel,
supported []fantasygoogle.ThinkingLevel,
) fantasygoogle.ThinkingLevel {
desiredRank := slices.Index(googleThinkingLevelsAscending, desired)
for _, candidate := range supported {
if slices.Index(googleThinkingLevelsAscending, candidate) >= desiredRank {
return candidate
}
}
return supported[len(supported)-1]
}
// googleThinkingLevel maps the global reasoning effort scale to Google
// thinking levels. Google offers no way to disable thinking on Gemini 3+
// models and no levels above HIGH, so the scale clamps at both ends.
func googleThinkingLevel(effort string) fantasygoogle.ThinkingLevel {
switch effort {
case codersdk.ChatModelReasoningEffortNone, codersdk.ChatModelReasoningEffortMinimal:
return fantasygoogle.ThinkingLevelMinimal
case codersdk.ChatModelReasoningEffortLow:
return fantasygoogle.ThinkingLevelLow
case codersdk.ChatModelReasoningEffortMedium:
return fantasygoogle.ThinkingLevelMedium
default:
return fantasygoogle.ThinkingLevelHigh
}
}
func ensureProviderOptions[T any, PT interface {
*T
fantasy.ProviderOptionsData
}](options fantasy.ProviderOptions, name string) PT {
providerOptions, _ := options[name].(PT)
if providerOptions == nil {
providerOptions = PT(new(T))
options[name] = providerOptions
}
return providerOptions
}