> ## 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.

# Configuration

> SDK configuration options and settings

This guide covers all configuration options for the RunAnywhere Swift SDK.

## SDK Initialization

For development (no API key needed):

```swift theme={null}
try RunAnywhere.initialize(environment: .development)
```

For production:

```swift theme={null}
try RunAnywhere.initialize(
    apiKey: "<YOUR_API_KEY>",
    baseURL: "https://api.runanywhere.ai",
    environment: .production
)
```

### Parameters

| Parameter     | Type             | Required   | Description                              |
| ------------- | ---------------- | ---------- | ---------------------------------------- |
| `apiKey`      | `String?`        | Production | API key for authentication               |
| `baseURL`     | `String?`        | Production | Backend API base URL                     |
| `environment` | `SDKEnvironment` | No         | Environment mode (default: .development) |

## SDKEnvironment

```swift theme={null}
public enum SDKEnvironment: String, Sendable {
    case development
    case staging
    case production
}
```

| Environment    | Log Level | Authentication | Analytics | Description                    |
| -------------- | --------- | -------------- | --------- | ------------------------------ |
| `.development` | Debug     | Optional       | Local     | Verbose logging, mock services |
| `.staging`     | Info      | Required       | Yes       | Testing with real services     |
| `.production`  | Warning   | Required       | Yes       | Minimal logging, full auth     |

## Module Registration

Register modules at app startup to enable capabilities:

```swift theme={null}
import RunAnywhere
import LlamaCPPRuntime
import ONNXRuntime

@MainActor
func configureSDK() {
    // Register LlamaCPP for LLM capability
    LlamaCPP.register(priority: 100)

    // Register ONNX for STT/TTS/VAD
    ONNX.register(priority: 100)
}
```

### Priority

Higher priority modules are preferred when multiple can handle a request:

```swift theme={null}
// Custom module with higher priority
MyCustomLLM.register(priority: 200)  // Used first
LlamaCPP.register(priority: 100)     // Fallback
```

## Model Registration

Register models before downloading or loading. Models require an ID, name, download URL, inference framework, and memory requirement.

### Single-File Model

```swift theme={null}
RunAnywhere.registerModel(
    id: String,
    name: String,
    url: URL,
    framework: .llamaCpp | .onnx | .coreml,
    modality: .speechRecognition | .speechSynthesis | .imageGeneration,  // optional
    artifactType: .archive(.tarGz | .zip, structure: .nestedDirectory),  // optional, for archives
    memoryRequirement: Int  // bytes
)
```

### Multi-File Model

For models with multiple files (e.g., VLM with a vision projector):

```swift theme={null}
RunAnywhere.registerMultiFileModel(
    id: String,
    name: String,
    files: [ModelFileDescriptor(url: URL, filename: String)],
    framework: .llamaCpp,
    modality: .multimodal,
    memoryRequirement: Int
)
```

### InferenceFramework

| Value       | Description                    |
| ----------- | ------------------------------ |
| `.llamaCpp` | LLM/VLM via llama.cpp (GGUF)   |
| `.onnx`     | STT/TTS/VAD via Sherpa-ONNX    |
| `.coreml`   | Apple CoreML (e.g., Diffusion) |

## Model Download and Loading

### Download with Progress

```swift theme={null}
let progressStream = try await RunAnywhere.downloadModel(modelId: String)
for await progress in progressStream {
    progress.overallProgress  // Double (0.0-1.0)
    progress.stage            // includes .completed
}
```

### Load Models by Modality

```swift theme={null}
try await RunAnywhere.loadModel(modelId: String)         // LLM
try await RunAnywhere.loadSTTModel(modelId: String)       // STT
try await RunAnywhere.loadTTSVoice(modelId: String)       // TTS
try await RunAnywhere.loadVLMModel(model: ModelDescriptor) // VLM (requires descriptor)
```

<Note>
  VLM model loading requires a `ModelDescriptor` object, not just a model ID. Fetch it via
  `RunAnywhere.availableModels()` and find the matching model.
</Note>

### Query Available Models

```swift theme={null}
let models = try await RunAnywhere.availableModels()
// Returns [ModelDescriptor] — each has .id, .name, .isDownloaded, .localPath
```

### Model State Checks

```swift theme={null}
await RunAnywhere.isModelLoaded           // Bool — LLM loaded?
await RunAnywhere.isSTTModelLoaded        // Bool — STT loaded?
await RunAnywhere.isTTSVoiceLoaded        // Bool — TTS loaded?
await RunAnywhere.isVLMModelLoaded        // Bool — VLM loaded?
await RunAnywhere.isDiffusionModelLoaded  // Bool — Diffusion loaded?
```

## LLM Generation Options

```swift theme={null}
public struct LLMGenerationOptions: Sendable {
    public let maxTokens: Int               // Maximum tokens to generate
    public let temperature: Float           // Randomness (0.0-2.0)
    public let topP: Float                  // Nucleus sampling (0.0-1.0)
    public let stopSequences: [String]      // Stop generation strings
    public let streamingEnabled: Bool       // Token streaming
    public let preferredFramework: InferenceFramework?
    public let structuredOutput: StructuredOutputConfig?
    public let systemPrompt: String?        // System prompt
}
```

### Default Values

| Option             | Default | Range/Type | Description                |
| ------------------ | ------- | ---------- | -------------------------- |
| `maxTokens`        | 100     | 1-4096+    | Max tokens to generate     |
| `temperature`      | 0.8     | 0.0-2.0    | Lower = deterministic      |
| `topP`             | 1.0     | 0.0-1.0    | Nucleus sampling threshold |
| `stopSequences`    | `[]`    | `[String]` | Stop on these strings      |
| `streamingEnabled` | false   | `Bool`     | Enable token streaming     |
| `systemPrompt`     | nil     | `String?`  | System instruction         |

### Example Configurations

```swift theme={null}
// Deterministic, factual responses
let factual = LLMGenerationOptions(
    maxTokens: 200,
    temperature: 0.1,
    topP: 0.9
)

// Creative writing
let creative = LLMGenerationOptions(
    maxTokens: 500,
    temperature: 1.2,
    topP: 0.95
)

// JSON output
let json = LLMGenerationOptions(
    maxTokens: 300,
    temperature: 0.2,
    stopSequences: ["}"],
    systemPrompt: "Respond only with valid JSON."
)
```

## STT Options

```swift theme={null}
public struct STTOptions: Sendable {
    public let language: String             // ISO 639-1 code
    public let sampleRate: Int              // Audio sample rate
    public let enableWordTimestamps: Bool   // Per-word timing
    public let enableVAD: Bool              // Voice activity detection
}
```

| Option                 | Default | Description                       |
| ---------------------- | ------- | --------------------------------- |
| `language`             | `"en"`  | Language code (or empty for auto) |
| `sampleRate`           | `16000` | Audio sample rate in Hz           |
| `enableWordTimestamps` | `false` | Include word timing info          |
| `enableVAD`            | `true`  | Filter silent segments            |

## TTS Options

```swift theme={null}
public struct TTSOptions: Sendable {
    public let rate: Float          // Speed (0.5-2.0)
    public let pitch: Float         // Pitch (0.5-2.0)
    public let volume: Float        // Volume (0.0-1.0)
    public let language: String     // Language code
    public let sampleRate: Int      // Output sample rate
    public let audioFormat: AudioFormat
}
```

| Option        | Default | Range   | Description        |
| ------------- | ------- | ------- | ------------------ |
| `rate`        | `1.0`   | 0.5-2.0 | Playback speed     |
| `pitch`       | `1.0`   | 0.5-2.0 | Voice pitch        |
| `volume`      | `1.0`   | 0.0-1.0 | Audio volume       |
| `sampleRate`  | `22050` | Hz      | Output sample rate |
| `audioFormat` | `.wav`  | Enum    | Output format      |

## VAD Configuration

```swift theme={null}
public struct VADConfiguration: Sendable {
    public let sampleRate: Int              // Audio sample rate
    public let frameLength: Double          // Frame duration (seconds)
    public let energyThreshold: Double      // Detection sensitivity
}
```

| Option            | Default | Description                         |
| ----------------- | ------- | ----------------------------------- |
| `sampleRate`      | `16000` | Audio sample rate                   |
| `frameLength`     | `0.032` | Frame length (32ms)                 |
| `energyThreshold` | `0.5`   | Sensitivity (0=sensitive, 1=strict) |

## Memory Considerations

### Model Memory Usage

| Model Type         | Typical Memory | Notes                    |
| ------------------ | -------------- | ------------------------ |
| LLM Q4 (1B params) | 1-2 GB         | Suitable for all devices |
| LLM Q4 (3B params) | 3-4 GB         | iPhone Pro / iPad        |
| LLM Q4 (7B params) | 6-8 GB         | M1+ Macs only            |
| STT (Whisper Base) | \~150 MB       | Universal                |
| TTS (Piper)        | 50-100 MB      | Universal                |

### Memory Management

```swift theme={null}
// Unload models when not needed
try await RunAnywhere.unloadModel()
try await RunAnywhere.unloadSTTModel()
try await RunAnywhere.unloadTTSVoice()

// Check storage
let storage = await RunAnywhere.getStorageInfo()
print("Available: \(storage.availableBytes / 1_000_000_000) GB")

// Clean temporary files
try await RunAnywhere.cleanTempFiles()
```

## Logging Configuration

```swift theme={null}
// Set log level
RunAnywhere.setLogLevel(.debug)  // .debug | .info | .warning | .error | .fault

// Enable local logging (Pulse integration)
RunAnywhere.configureLocalLogging(enabled: true)

// Enable verbose debug mode
RunAnywhere.setDebugMode(true)

// Flush logs
await RunAnywhere.flushAll()
```

### Log Levels

| Level      | Description                                   |
| ---------- | --------------------------------------------- |
| `.debug`   | Detailed information for debugging            |
| `.info`    | General operational information               |
| `.warning` | Potential issues that don't prevent operation |
| `.error`   | Errors that affect specific operations        |
| `.fault`   | Critical errors indicating serious problems   |

## SDK State

```swift theme={null}
// Check initialization
let isInitialized = RunAnywhere.isSDKInitialized
let servicesReady = RunAnywhere.areServicesReady
let isActive = RunAnywhere.isActive

// Version info
let version = RunAnywhere.version

// Authentication
let isAuthenticated = RunAnywhere.isAuthenticated
let userId = RunAnywhere.getUserId()
let orgId = RunAnywhere.getOrganizationId()

// Device
let deviceId = RunAnywhere.deviceId
let isRegistered = RunAnywhere.isDeviceRegistered()
```

## Environment-Specific Settings

```swift theme={null}
func configureForEnvironment() {
    #if DEBUG
    try? RunAnywhere.initialize(
        environment: .development
    )
    RunAnywhere.setLogLevel(.debug)
    RunAnywhere.setDebugMode(true)
    #else
    try? RunAnywhere.initialize(
        apiKey: ProcessInfo.processInfo.environment["RUNANYWHERE_API_KEY"],
        baseURL: "https://api.runanywhere.ai",
        environment: .production
    )
    RunAnywhere.setLogLevel(.warning)
    #endif
}
```

## Reset (Testing)

```swift theme={null}
// Reset SDK state for testing
RunAnywhere.reset()
```

<Warning>
  Only use `reset()` in development/testing. It clears all SDK state including authentication.
</Warning>

<CardGroup cols={2}>
  <Card title="Error Handling" icon="triangle-exclamation" href="/swift/error-handling">
    Handle SDK errors →
  </Card>

  <Card title="Best Practices" icon="lightbulb" href="/swift/best-practices">
    Performance tips →
  </Card>
</CardGroup>
