Skip to main content

SDK Initialization

On Android, initialization requires three steps in this exact order:
import com.runanywhere.sdk.storage.AndroidPlatformContext
import com.runanywhere.sdk.foundation.bridge.extensions.CppBridgeModelPaths

// 1. Initialize Android platform context (must be first)
AndroidPlatformContext.initialize(context)

// 2. Initialize SDK
RunAnywhere.initialize(environment = SDKEnvironment.DEVELOPMENT)

// 3. Set model storage base directory
val runanywherePath = context.filesDir.resolve("runanywhere").also { it.mkdirs() }
CppBridgeModelPaths.setBaseDirectory(runanywherePath.absolutePath)
AndroidPlatformContext.initialize() must be called before RunAnywhere.initialize(). The CppBridgeModelPaths.setBaseDirectory() call tells the native C++ bridge where model files are stored on disk.

SDKEnvironment

EnvironmentLog LevelDescription
DEVELOPMENTDebugFull logging, local testing
STAGINGInfoStaging backend, moderate logging
PRODUCTIONWarningProduction backend, minimal logging

LLM Generation Options

data class LLMGenerationOptions(
    val maxTokens: Int = 100,           // Maximum tokens to generate
    val temperature: Float = 0.8f,      // Creativity (0.0-2.0)
    val topP: Float = 1.0f,             // Nucleus sampling (0.0-1.0)
    val stopSequences: List<String> = emptyList(),
    val streamingEnabled: Boolean = false,
    val preferredFramework: InferenceFramework? = null,
    val systemPrompt: String? = null
)

Temperature Guide

ValueUse CaseDescription
0.0-0.3Code, factsDeterministic, consistent
0.4-0.7General Q&ABalanced
0.8-1.0Creative writingMore variety
1.1-2.0BrainstormingHigh creativity

STT Options

data class STTOptions(
    val language: String = "en",
    val detectLanguage: Boolean = false,
    val enablePunctuation: Boolean = true,
    val enableDiarization: Boolean = false,
    val maxSpeakers: Int? = null,
    val enableTimestamps: Boolean = true,
    val vocabularyFilter: List<String> = emptyList(),
    val audioFormat: AudioFormat = AudioFormat.PCM,
    val sampleRate: Int = 16000
)

TTS Options

data class TTSOptions(
    val voice: String? = null,
    val language: String = "en-US",
    val rate: Float = 1.0f,          // 0.0-2.0
    val pitch: Float = 1.0f,         // 0.0-2.0
    val volume: Float = 1.0f,        // 0.0-1.0
    val audioFormat: AudioFormat = AudioFormat.PCM,
    val sampleRate: Int = 22050,
    val useSSML: Boolean = false
)

VAD Configuration

data class VADConfiguration(
    val threshold: Float = 0.5f,
    val minSpeechDurationMs: Int = 250,
    val minSilenceDurationMs: Int = 300,
    val sampleRate: Int = 16000,
    val frameSizeMs: Int = 30
)

Memory Considerations

Model memory requirements vary by size and quantization:
Model TypeMemoryDevice Requirements
LLM 0.5B Q8~500MBAny Android 7.0+
LLM 0.5B Q4~300MBAny Android 7.0+
LLM 1B Q4~600MB4GB+ RAM
LLM 3B Q4~2GB6GB+ RAM
Whisper Tiny~75MBAny device
Whisper Base~150MBAny device
TTS Voice~50MBAny device
Always check available memory before loading large models. Loading models that exceed available RAM will crash the app.

Model Registry

Register custom models:
val modelInfo = RunAnywhere.registerModel(
    id = "my-custom-model",           // Optional, generated from URL if null
    name = "My Custom Model",
    url = "https://...",
    framework = InferenceFramework.LLAMA_CPP,
    modality = ModelCategory.LANGUAGE,
    memoryRequirement = 500_000_000L,  // 500MB
    supportsThinking = false
)

InferenceFramework

enum class InferenceFramework {
    ONNX,              // ONNX Runtime (STT/TTS/VAD)
    LLAMA_CPP,         // llama.cpp (LLM)
    FOUNDATION_MODELS, // Platform foundation models
    SYSTEM_TTS,        // System text-to-speech
    BUILT_IN,          // Simple built-in services
    NONE,              // No model needed
    UNKNOWN
}

ModelCategory

enum class ModelCategory {
    LANGUAGE,              // LLMs (text-to-text)
    SPEECH_RECOGNITION,    // STT (voice-to-text)
    SPEECH_SYNTHESIS,      // TTS (text-to-voice)
    VISION,                // Image understanding
    IMAGE_GENERATION,      // Text-to-image
    MULTIMODAL,            // Multiple modalities
    AUDIO                  // Audio processing
}

SDK Lifecycle

// Check initialization state
val isInit = RunAnywhere.isInitialized
val servicesReady = RunAnywhere.areServicesReady

// Get SDK version
val version = RunAnywhere.version

// Get current environment
val env = RunAnywhere.environment

// Reset SDK (clear all state)
RunAnywhere.reset()

// Cleanup resources without full reset
RunAnywhere.cleanup()