getMessage() ); // Match on descriptive words/codes rather than bare numbers: a needle like // '429' or '401' would collide with digits inside token counts or request // ids (e.g. 'input token count (34291)') and mis-route the guidance. $has = static function ( array $needles ) use ( $text ): bool { foreach ( $needles as $needle ) { if ( false !== strpos( $text, $needle ) ) { return true; } } return false; }; // Context window exceeded — the conversation is too long. Providers phrase // this differently (OpenAI 'context_length_exceeded', Google 'input token // count … exceeds the maximum number of tokens'), so match several forms. if ( $has( [ 'context_length_exceeded', 'context length', 'context window', 'maximum context', 'maximum number of tokens', 'exceeds the maximum', 'too many tokens', 'prompt is too long', 'reduce the length', ] ) ) { return __( 'This conversation has grown too long for the current AI model to process. Please start a new chat to continue. Choosing a model with a larger context window in Settings can also help.', 'aisentic' ); } // Billing / quota exhausted — a persistent condition that waiting will not // fix; the account needs attention. Checked before the rate-limit branch so // 'insufficient_quota' is not mistaken for a transient throttle. if ( $has( [ 'insufficient_quota', 'exceeded your current quota', 'billing', 'payment required', 'check your plan', 'used all of this month', ] ) ) { return __( 'Your Aisentic AI plan has run out of available usage. Please check your plan and billing details, then try again once more usage is available.', 'aisentic' ); } // Rate limited / overloaded — transient; retrying shortly usually works. if ( $has( [ 'rate limit', 'rate_limit', 'ratelimit', 'too many requests', 'resource_exhausted', 'resource has been exhausted', 'overloaded', ] ) ) { return __( 'The AI service is handling too many requests right now. Please wait a few seconds and try again.', 'aisentic' ); } if ( $has( [ 'timed out', 'timeout', 'curl error 28', 'network error', 'operation timed out' ] ) ) { return __( 'The AI service took too long to respond. Please try again — breaking a large task into smaller steps can help.', 'aisentic' ); } if ( $has( [ 'invalid_api_key', 'incorrect api key', 'invalid authentication', 'unauthorized', 'authentication' ] ) ) { return __( 'The Aisentic AI connection is not authorized. Please reconnect your account from Settings.', 'aisentic' ); } return $generic; } }