provider-error.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { Schema } from "effect"
  2. import { LLMError, ProviderErrorEvent } from "./schema"
  3. const patterns = [
  4. /prompt is too long/i,
  5. /request_too_large/i,
  6. /input is too long for requested model/i,
  7. /exceeds the context window/i,
  8. /exceeds (?:the )?(?:model'?s )?maximum context length(?: of [\d,]+ tokens?|\s*\([\d,]+\))/i,
  9. /input token count.*exceeds the maximum/i,
  10. /tokens in request more than max tokens allowed/i,
  11. /maximum prompt length is \d+/i,
  12. /reduce the length of the messages/i,
  13. /maximum context length is \d+ tokens/i,
  14. /exceeds (?:the )?maximum allowed input length of [\d,]+ tokens?/i,
  15. /input \(\d+ tokens\) is longer than the model'?s context length \(\d+ tokens\)/i,
  16. /exceeds the limit of \d+/i,
  17. /exceeds the available context size/i,
  18. /greater than the context length/i,
  19. /context window exceeds limit/i,
  20. /exceeded model token limit/i,
  21. /context[_ ]length[_ ]exceeded/i,
  22. /request entity too large/i,
  23. /context length is only \d+ tokens/i,
  24. /input length.*exceeds.*context length/i,
  25. /prompt too long; exceeded (?:max )?context length/i,
  26. /too large for model with \d+ maximum context length/i,
  27. /prompt has [\d,]+ tokens?, but the configured context size is [\d,]+ tokens?/i,
  28. /model_context_window_exceeded/i,
  29. /too many tokens/i,
  30. /token limit exceeded/i,
  31. ]
  32. const exclusions = [/^(throttling error|service unavailable):/i, /rate limit/i, /too many requests/i]
  33. export const isContextOverflow = (message: string) =>
  34. !exclusions.some((pattern) => pattern.test(message)) &&
  35. (patterns.some((pattern) => pattern.test(message)) || /^4(00|13)\s*(status code)?\s*\(no body\)/i.test(message))
  36. export const isContextOverflowFailure = (failure: unknown) =>
  37. failure instanceof LLMError
  38. ? failure.reason._tag === "InvalidRequest" && failure.reason.classification === "context-overflow"
  39. : Schema.is(ProviderErrorEvent)(failure) && failure.classification === "context-overflow"