Path: redb-route/redb.Route.Llm/Providers/AnthropicModelProfile.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm/Providers/AnthropicModelProfile.cs
----
namespace redb.Route.Llm.Providers;
///
/// Contract generation of an Anthropic model, as it affects how a Messages-API
/// request must be shaped. The Anthropic wire contract changed across generations:
/// current-generation models reject the sampling knobs
/// (temperature/top_p/top_k) with HTTP 400, whereas older
/// generations accept them. The tier drives request building so the provider never
/// sends a field the target model will reject.
///
public enum AnthropicModelTier
{
///
/// Older generation (Sonnet 4.5 / Haiku 4.5 and earlier, Claude 3.x).
/// Accepts temperature/top_p/top_k.
///
Legacy,
///
/// Transitional generation (Opus 4.6 / Sonnet 4.6). Still accepts sampling knobs.
///
Transitional,
///
/// Current generation (Opus 4.7 / 4.8 / 5, Sonnet 5, Fable 5, and — by the
/// fail-forward default — any unrecognised newer id). Rejects sampling
/// knobs with HTTP 400.
///
Modern
}
///
/// How the target model accepts the sampling knobs (temperature/top_p).
/// The contract is not binary — it has three states across generations:
///
public enum AnthropicSamplingPolicy
{
/// Claude 3.x — temperature and top_p may both be sent.
Both,
///
/// Claude 4.0–4.6 (incl. Sonnet/Haiku 4.5 and Opus/Sonnet 4.6) — at most one
/// of temperature/top_p; sending both returns HTTP 400.
///
AtMostOne,
///
/// Claude 4.7+ / 5 — non-default sampling is rejected with HTTP 400 (omitting the
/// field, or passing its default, is accepted). The connector omits them entirely.
///
None
}
///
/// Capability descriptor for an Anthropic model: which request fields the target
/// model accepts. Resolved from the model id by using a static
/// generation table plus a fail-forward default — an unrecognised id is
/// treated as so a future model release does
/// not get sent a now-removed field and 400.
///
/// This is Anthropic-scoped on purpose (the OpenAI-compatible providers accept
/// sampling and need no such gating). Generalise to an IModelProfile only if a
/// second provider ever needs per-model request shaping.
///
///
public readonly record struct AnthropicModelProfile(AnthropicModelTier Tier, AnthropicSamplingPolicy Sampling)
{
///
/// Whether any sampling knob may be sent at all. only for
/// (current-generation models — sending
/// temperature/top_p yields HTTP 400). Note that
/// still allows one knob;
/// consult directly to shape the request.
///
public bool SamplingSupported => Sampling != AnthropicSamplingPolicy.None;
///
/// Resolves the profile for . When
/// is a recognised tier name
/// (legacy/transitional/modern, case-insensitive) it wins —
/// the escape-hatch for self-hosted ids, proxies, or snapshots the table does not
/// know. Otherwise the id is classified by generation, defaulting to
/// when no version can be read.
///
public static AnthropicModelProfile Resolve(string? modelId, string? tierOverride = null)
{
if (TryParseTier(tierOverride, out var forced))
// An explicit tier override maps to the safe sampling policy for that tier:
// any sampling-accepting model tolerates a single knob, so AtMostOne is the
// safe superset for legacy/transitional; modern omits sampling.
return new AnthropicModelProfile(
forced,
forced == AnthropicModelTier.Modern
? AnthropicSamplingPolicy.None
: AnthropicSamplingPolicy.AtMostOne);
TryReadVersion(modelId, out var major, out var minor);
return new AnthropicModelProfile(
ClassifyByGeneration(major, minor),
ClassifySampling(major, minor));
}
/// Parses an explicit tier-name override; returns false when unset/unknown.
public static bool TryParseTier(string? name, out AnthropicModelTier tier)
{
switch (name?.Trim().ToLowerInvariant())
{
case "legacy": tier = AnthropicModelTier.Legacy; return true;
case "transitional": tier = AnthropicModelTier.Transitional; return true;
case "modern": tier = AnthropicModelTier.Modern; return true;
default: tier = AnthropicModelTier.Modern; return false;
}
}
///
/// Classifies a (major, minor) version into a contract tier. Rules:
///
/// - major ≥ 5 → Modern (Opus/Sonnet/Fable 5);
/// - major 4, minor ≥ 7 → Modern (Opus 4.7 / 4.8);
/// - major 4, minor == 6 → Transitional (Opus/Sonnet 4.6);
/// - major 4, minor ≤ 5 → Legacy (Sonnet 4.5 / Haiku 4.5);
/// - major ≤ 3 → Legacy (Claude 3.x);
/// - no version (major < 0) → Modern (fail-forward for future ids).
///
///
private static AnthropicModelTier ClassifyByGeneration(int major, int minor)
{
if (major < 0) return AnthropicModelTier.Modern; // unknown/future id — assume current contract
if (major >= 5) return AnthropicModelTier.Modern;
if (major == 4)
{
if (minor >= 7) return AnthropicModelTier.Modern; // 4.7, 4.8
if (minor == 6) return AnthropicModelTier.Transitional; // 4.6
return AnthropicModelTier.Legacy; // 4.5 and lower, or none
}
return AnthropicModelTier.Legacy; // 3.x and older
}
///
/// Classifies a (major, minor) version into a sampling policy — the state that
/// actually shapes the wire request:
///
/// - major ≤ 3 → (Claude 3.x accepts both knobs);
/// - major 4, minor ≤ 6 → (Claude 4.0–4.6: both → 400);
/// - major 4, minor ≥ 7 or major ≥ 5 → (4.7+/5 reject non-default sampling);
/// - no version (major < 0) → (fail-forward).
///
///
private static AnthropicSamplingPolicy ClassifySampling(int major, int minor)
{
if (major < 0) return AnthropicSamplingPolicy.None; // unknown/future id
if (major <= 3) return AnthropicSamplingPolicy.Both; // Claude 3.x
if (major == 4 && minor <= 6) return AnthropicSamplingPolicy.AtMostOne; // 4.0–4.6
return AnthropicSamplingPolicy.None; // 4.7+ / 5
}
///
/// Extracts (major, minor) from a model id. Normalises '.'→'-', lowercases, and
/// treats the first two short numeric tokens (1–2 digits) as major/minor —
/// skipping long tokens such as an 8-digit date snapshot.
/// is -1 when absent. Returns false when no version token is present.
///
private static bool TryReadVersion(string? modelId, out int major, out int minor)
{
major = -1;
minor = -1;
if (string.IsNullOrWhiteSpace(modelId)) return false;
var normalized = modelId.Replace('.', '-');
foreach (var token in normalized.Split('-', StringSplitOptions.RemoveEmptyEntries))
{
// Version components are 1–2 digits; longer all-digit tokens are dates (e.g. 20251001).
if (token.Length is < 1 or > 2) continue;
if (!int.TryParse(token, out var n)) continue;
if (major < 0) { major = n; continue; }
minor = n;
break;
}
return major >= 0;
}
}