Skip to content

Error Handling

Error Types#

All Cognest errors extend CognestError and include a machine-readable error code:

Error ClassCodeDescription
AuthenticationErrorAUTH_FAILEDInvalid or expired API key
IntegrationErrorINTEGRATION_*Integration-specific failures
RateLimitErrorRATE_LIMITEDAPI rate limit exceeded
ThinkEngineErrorENGINE_*LLM provider errors
SkillErrorSKILL_*Skill execution failures
ConfigErrorCONFIG_INVALIDInvalid configuration
WebhookErrorWEBHOOK_*Webhook verification or processing failures

Catching Errors#

error-handling.ts
import { Cognest, CognestError, RateLimitError, IntegrationError } from '@cognest/sdk'

const cognest = new Cognest()

try {
  const reply = await cognest.think('Hello!')
  await cognest.integration('whatsapp').send(userId, reply)
} catch (error) {
  if (error instanceof RateLimitError) {
    console.log(`Rate limited. Retry after ${error.retryAfter}ms`)
    // Automatic retry is built-in, but you can handle manually
  } else if (error instanceof IntegrationError) {
    console.log(`Integration ${error.integration} failed: ${error.message}`)
    console.log(`Error code: ${error.code}`)
  } else if (error instanceof CognestError) {
    console.log(`Cognest error [${error.code}]: ${error.message}`)
  } else {
    throw error
  }
}

Global Error Handler#

global-errors.ts
// Catch all unhandled errors
cognest.on('error', (error) => {
  console.error(`[Global] ${error.code}: ${error.message}`)

  // Send to your error tracking service
  Sentry.captureException(error, {
    tags: {
      cognest_code: error.code,
      integration: error.integration ?? 'none',
    },
  })
})

// Catch per-integration errors
cognest.integration('whatsapp').on('error', (error) => {
  console.error(`[WhatsApp] ${error.message}`)
})

Retry Configuration#

Cognest includes automatic retry with exponential backoff for transient errors:

cognest.config.yaml
# cognest.config.yaml
server:
  retry:
    max_attempts: 3           # Maximum retry attempts
    initial_delay: 1000       # First retry delay (ms)
    max_delay: 30000          # Maximum delay between retries
    backoff_multiplier: 2     # Exponential backoff factor
    retryable_codes:          # Only retry these error types
      - RATE_LIMITED
      - INTEGRATION_TIMEOUT
      - ENGINE_OVERLOADED

Error Codes Reference#

Authentication Errors#

CodeCauseResolution
AUTH_FAILEDInvalid API keyCheck COGNEST_API_KEY is correct
AUTH_EXPIREDToken has expiredRefresh tokens via cognest.auth.refresh()
AUTH_INSUFFICIENT_SCOPEMissing permissionsRequest additional OAuth scopes

Integration Errors#

CodeCauseResolution
INTEGRATION_NOT_FOUNDIntegration not configuredAdd integration to config or register with cognest.use()
INTEGRATION_TIMEOUTPlatform API timed outRetry or check platform status
INTEGRATION_AUTH_FAILEDPlatform credentials invalidUpdate integration credentials
INTEGRATION_RATE_LIMITEDPlatform rate limit hitReduce request frequency

Engine Errors#

CodeCauseResolution
ENGINE_PROVIDER_ERRORLLM provider returned an errorCheck provider status or try a different model
ENGINE_OVERLOADEDProvider is overloadedRetry with backoff
ENGINE_CONTEXT_TOO_LONGInput exceeds context windowReduce max_history or truncate input
ENGINE_INVALID_RESPONSEModel returned unparseable outputAdjust system prompt or temperature