a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
56 lines
1.9 KiB
Go
56 lines
1.9 KiB
Go
// Package llm — context-overflow classification for adaptive retry.
|
|
//
|
|
// When a request's prompt exceeds the model's context window, every
|
|
// provider fails it — but each phrases the error differently (the
|
|
// HTTP providers relay their API's wording, the local llama.cpp
|
|
// provider its own, the CLI providers whatever their binary printed).
|
|
// IsContextOverflow recognises that failure mode across all of them so
|
|
// the assist layer can react to it (see the svc package's adaptive
|
|
// chunk-bisection retry) instead of surfacing a hard error.
|
|
package llm
|
|
|
|
import "strings"
|
|
|
|
// contextOverflowMarkers are lowercase substrings that appear in a
|
|
// provider error when a request exceeds the model's context window.
|
|
// The set is deliberately specific — generic words like "context" or
|
|
// "tokens" alone would misclassify unrelated failures. Covers the
|
|
// Anthropic / OpenAI / Ollama / Gemini / Bedrock / DeepSeek API
|
|
// wordings and the local llama.cpp `n_ctx` message.
|
|
var contextOverflowMarkers = []string{
|
|
"context length",
|
|
"context window",
|
|
"context_length_exceeded",
|
|
"maximum context",
|
|
"exceeds the maximum",
|
|
"exceeds the context",
|
|
"exceed context",
|
|
"too many tokens",
|
|
"too many input tokens",
|
|
"prompt is too long",
|
|
"input is too long",
|
|
"input too long",
|
|
"reduce the length",
|
|
"please reduce",
|
|
"token limit",
|
|
"n_ctx",
|
|
}
|
|
|
|
// IsContextOverflow reports whether err looks like a model
|
|
// context-window overflow — the prompt was too large for the model to
|
|
// accept. It is a best-effort string classifier: providers expose no
|
|
// structured error code for this, so the assist layer treats a true
|
|
// result as "retry smaller" and a false result as a genuine failure.
|
|
func IsContextOverflow(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
msg := strings.ToLower(err.Error())
|
|
for _, m := range contextOverflowMarkers {
|
|
if strings.Contains(msg, m) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|