Error handling

Read the Askocto API v2 error object, the list of error codes and the right way to retry rate limited and failed requests.

Working draft. These docs are illustrative: names, endpoints and examples describe the intended behaviour and can change before launch.

When a request fails, the API returns a non-2xx HTTP status and a JSON body with an error object. Read the code in your program and show the message to people only after you check it is safe to expose.

The error object

{
  "error": {
    "code": "not_found",
    "message": "Agent not found.",
    "requestId": "req_7m3x1d"
  }
}
Field Type Description
code string A stable, machine-readable code. Branch on this field.
message string A short description for developers. The wording can change.
requestId string The ID of the request, starting with req_. Quote it when you contact support.

Error codes

Status Code Meaning
400 invalid_request A parameter or body field is missing, has the wrong type or has an unknown value.
401 unauthorized The API key is missing, malformed or deleted. See Authentication.
403 forbidden The key is valid, but the action is not allowed.
404 not_found The resource does not exist, or the ID belongs to another workspace.
409 conflict The request clashes with the current state, for example a duplicate.
429 rate_limited You sent too many requests. The Retry-After header says how many seconds to wait.
500 server_error Something failed on our side.

Errors in API v1 use the same statuses and codes with a flatter body.

Which errors to retry

  • 400, 401, 403, 404 and 409 will fail again with the same request. Fix the request instead.
  • 429: wait for the number of seconds in Retry-After, then send the request again.
  • 500 and other 5xx: retry with exponential backoff and a little random jitter, and stop after a few attempts.

A 5xx response can arrive after the work has started. Before you retry a POST that creates something, check whether the first call went through, for example by listing the resource.

Retry helper

async function callApi(path, options = {}, attempt = 0) {
  const response = await fetch(`https://api.askocto.co${path}`, {
    ...options,
    headers: {
      Authorization: `Bearer ${process.env.OCTO_API_KEY}`,
      "Content-Type": "application/json",
      ...options.headers,
    },
  })

  if (response.ok) return response.json()

  const retryable = response.status === 429 || response.status >= 500
  if (retryable && attempt < 4) {
    const retryAfter = Number(response.headers.get("Retry-After"))
    const delay = retryAfter > 0 ? retryAfter * 1000 : 2 ** attempt * 500 + Math.random() * 250
    await new Promise((resolve) => setTimeout(resolve, delay))
    return callApi(path, options, attempt + 1)
  }

  const { error } = await response.json()
  throw new Error(`${error.code}: ${error.message} (${error.requestId})`)
}

const agents = await callApi("/v2/agents?limit=5")

Errors while streaming

A request that fails before the stream starts returns a normal JSON error with an HTTP status. A failure after the stream has started arrives as an error event with the same error object, see Streaming. Each endpoint page lists the errors that apply to it, and the API v2 overview covers the rest of the conventions.

Something unclear or missing? Tell us and we will fix the page.