Cortex

Error Handling

Sentinel errors returned by Cortex operations.

Cortex defines sentinel errors in the root cortex package. All errors are created with errors.New and can be checked with errors.Is.

Store errors

ErrorDescription
ErrNoStoreNo store was configured on the engine
ErrStoreClosedThe store has been closed
ErrMigrationFailedDatabase migration failed

Not-found errors

ErrorDescription
ErrAgentNotFoundAgent with the given ID does not exist
ErrRunNotFoundRun with the given ID does not exist
ErrStepNotFoundStep with the given ID does not exist
ErrToolCallNotFoundTool call with the given ID does not exist
ErrSkillNotFoundSkill with the given ID does not exist
ErrTraitNotFoundTrait with the given ID does not exist
ErrBehaviorNotFoundBehavior with the given ID does not exist
ErrPersonaNotFoundPersona with the given ID does not exist
ErrCheckpointNotFoundCheckpoint with the given ID does not exist

Conflict errors

ErrorDescription
ErrAlreadyExistsA resource with the same name already exists in this app

State errors

ErrorDescription
ErrInvalidStateInvalid state transition (e.g., completing an already-failed run)
ErrRunCancelledThe run was cancelled
ErrRunAlreadyDoneThe run has already completed
ErrBudgetExhaustedToken or cost budget exhausted
ErrMaxStepsReachedMaximum reasoning steps reached
ErrMaxTokensReachedMaximum output tokens reached

Error wrapping

Store implementations wrap these sentinel errors with additional context:

return fmt.Errorf("postgres: get agent %s: %w", agentID, cortex.ErrAgentNotFound)

Check errors using errors.Is:

agent, err := eng.GetAgent(ctx, agentID)
if errors.Is(err, cortex.ErrAgentNotFound) {
    // handle not found
}

API error mapping

The HTTP API maps sentinel errors to HTTP status codes:

Sentinel errorHTTP status
ErrAgentNotFound, ErrRunNotFound, etc.404 Not Found
ErrAlreadyExists409 Conflict
ErrInvalidState, ErrRunCancelled, etc.400 Bad Request
ErrNoStore500 Internal Server Error

On this page