> ## Documentation Index
> Fetch the complete documentation index at: https://docs.runanywhere.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Error Handling

> SDKException, codes, and recovery

Everything the SDK throws is an `SDKException`, wrapping a proto-backed error so the same code
and category cross every language binding.

```ts theme={"theme":"css-variables"}
import { SDKException, isSDKException } from '@runanywhere/web'

try {
  const result = await RunAnywhere.llm.generate(prompt)
} catch (error) {
  if (isSDKException(error)) {
    console.log(error.code, error.message)
  }
}
```

`isSDKException` is the type guard to use. A bare `instanceof` can fail across bundle
boundaries when two copies of the package end up in one page.

## Codes worth handling

| Code | Meaning | What to do |
| - | - | - |
| `notInitialized` | Called before `initialize()` | Initialize first |
| `modelNotFound` | No such model | Check the id, or register it |
| `networkUnavailable` | Download needs a connection | Retry when online |
| `insufficientStorage` | Browser storage quota exceeded | Free space, or ask for a directory |
| `insufficientMemory` | Model will not fit | Unload something, or use a smaller model |
| `microphonePermissionDenied` | No mic access | Ask again from a user gesture |
| `timeout` | Took too long | Retry |
| `invalidApiKey` | Credentials rejected | Check the key |
| `cancelled` | The caller cancelled | Usually ignore |
| `invalidState` | Wrong order of operations | Read the message |
| `unsupportedCapability` | Not implemented in this build | See below |

## unsupportedCapability

Two cases you will actually meet:

```ts theme={"theme":"css-variables"}
// Throws: constrained decoding is not wired in
await RunAnywhere.llm.generateStructured(prompt, schema, 'constrained')
```

```ts theme={"theme":"css-variables"}
// Throws: the web index is process-wide
const a = await RunAnywhere.rag.open(embeddingModel)
const b = await RunAnywhere.rag.open(embeddingModel)
// invalidState: "A RAG session is already open. Close it before opening another"
```

## Browser-specific failures

Some failures are the browser, not the SDK, and reading them as SDK errors sends you the wrong
way:

| Symptom | Cause |
| - | - |
| `SharedArrayBuffer is not defined` | The page is not cross-origin isolated. Add COOP and COEP. |
| Microphone request rejected | Not called from a user gesture, or not a secure context |
| `chooseDirectory()` rejected | Same: needs a user gesture |
| Audio plays silently | Autoplay policy. Start from a click. |
| Storage quota errors | The origin's quota, not the disk |

## Errors in streams

Preflight throws from the call. A failure **during** generation arrives as a `failed` event
rather than an exception:

```ts theme={"theme":"css-variables"}
try {
  for await (const event of RunAnywhere.llm.generateStream(prompt)) {
    if (event.type === 'failed') {
      showRetry(event.error)
      break
    }
    if (event.type === 'textDelta') output.textContent += event.text
  }
} catch (error) {
  // preflight only
  if (isSDKException(error)) showFatal(error)
}
```

This is the opposite of React Native, where in-flight failures are thrown into the consumer.
Do not share one error-handling helper between the two without checking.

## Cancellation

Breaking out of the loop cancels the request and is not an error.

```ts theme={"theme":"css-variables"}
for await (const event of RunAnywhere.llm.generateStream(prompt)) {
  if (aborted) break
  if (event.type === 'textDelta') text += event.text
}
```

## Reporting

```ts theme={"theme":"css-variables"}
catch (error) {
  if (isSDKException(error)) {
    analytics.track('sdk_error', { code: error.code, category: error.category });
  } else {
    throw error;
  }
}
```

Rethrow what you did not recognise. Swallowing every error in a `catch` is how a broken build
looks like a working one.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.