> ## 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`. It wraps a proto-backed error, so the same code
and category cross every language binding.

```kotlin theme={"theme":"css-variables"}
try {
    val result = RunAnywhere.llm.generate(prompt)
} catch (e: SDKException) {
    println(e.code)
    println(e.errorDescription)
    println(e.recoverySuggestion)
}
```

## SDKException

| Member | Type | Meaning |
| - | - | - |
| `code` | `ProtoErrorCode` | What went wrong |
| `category` | `ProtoErrorCategory` | Which layer it came from |
| `errorDescription` | `String?` | Human-readable description |
| `failureReason` | `String` | Why it failed |
| `recoverySuggestion` | `String?` | What the user can do |
| `fieldPath` | `String?` | Which input failed validation |
| `cAbiCode` | `Int?` | Underlying C ABI code |
| `nestedMessage` | `String?` | Message from a wrapped error |
| `stackTraceSnapshot` | `List<String>` | Capture site |
| `telemetryProperties` | `Map<String, String>` | Ready-made reporting payload |

## Codes worth handling

| Code | Meaning | What to do |
| - | - | - |
| `ERROR_CODE_NOT_INITIALIZED` | Called before `initialize()` | Initialize first |
| `ERROR_CODE_MODEL_NOT_FOUND` | No such model | Check the id, or register it |
| `ERROR_CODE_NETWORK_UNAVAILABLE` | Download needs a connection | Retry when online |
| `ERROR_CODE_INSUFFICIENT_STORAGE` | Not enough disk | Free space or use a smaller model |
| `ERROR_CODE_INSUFFICIENT_MEMORY` | Model will not fit in RAM | Unload something, or use a smaller model |
| `ERROR_CODE_MICROPHONE_PERMISSION_DENIED` | No mic access | Send the user to Settings |
| `ERROR_CODE_TIMEOUT` | Took too long | Retry |
| `ERROR_CODE_INVALID_API_KEY` | Credentials rejected | Check the key |
| `ERROR_CODE_CANCELLED` | The caller cancelled | Usually ignore |
| `ERROR_CODE_INVALID_ARGUMENT` | An input was rejected | Read `fieldPath` |

## Factory methods

The companion object builds typed exceptions, which is what the SDK itself throws and what you
can throw from your own tool executors:

```kotlin theme={"theme":"css-variables"}
SDKException.notInitialized(component)
SDKException.invalidConfiguration(message)
SDKException.unsupportedCapability(name, reason)
SDKException.invalidApiKey()
SDKException.invalidArgument(message)
SDKException.validationFailed(message)
SDKException.modelNotFound(modelId)
```

`unsupportedCapability` is the one you will meet in practice: it is what
`generateStructured(mode = CONSTRAINED)` throws.

## Reacting to specific codes

```kotlin theme={"theme":"css-variables"}
try {
    val result = RunAnywhere.llm.generate(prompt)
} catch (e: SDKException) {
    when (e.code) {
        ProtoErrorCode.ERROR_CODE_NOT_INITIALIZED ->
            RunAnywhere.initialize(apiKey = "", baseUrl = "")
        ProtoErrorCode.ERROR_CODE_INSUFFICIENT_MEMORY ->
            RunAnywhere.models.unloadAll()
        ProtoErrorCode.ERROR_CODE_CANCELLED -> Unit
        else -> show(e.errorDescription ?: e.failureReason)
    }
}
```

## Errors from a Flow

Failures surface at the collector, so wrap the collection:

```kotlin theme={"theme":"css-variables"}
try {
    RunAnywhere.llm.generateStream(prompt).collect { event ->
        if (event is GenerationEvent.Token) text += event.text
    }
} catch (e: SDKException) {
    show(e)
}
```

`catch` from the Flow API works too:

```kotlin theme={"theme":"css-variables"}
RunAnywhere.llm.generateStream(prompt)
    .catch { e -> if (e is SDKException) show(e) }
    .collect { event -> /* … */ }
```

## Cancellation

Cancelling the coroutine cancels the generation. That arrives as `CancellationException`, which
you must not swallow: rethrow it, or use `catch` from the Flow API, which already lets it
through.

```kotlin theme={"theme":"css-variables"}
try {
    RunAnywhere.llm.generateStream(prompt).collect { /* … */ }
} catch (e: CancellationException) {
    throw e
} catch (e: SDKException) {
    show(e)
}
```

## Reporting

```kotlin theme={"theme":"css-variables"}
catch (e: SDKException) {
    analytics.log("sdk_error", e.telemetryProperties)
}
```

`telemetryProperties` is already shaped for a reporting call, with the code, category, and
context attached.


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