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

# Best Practices

> Getting good behaviour out of on-device models

## Build to a device

The Android emulator has no arm64 native libraries for these backends, and MLX only runs on
physical iOS hardware. A simulator tells you the app launches and nothing more.

## Register a backend before initializing

```dart theme={"theme":"css-variables"}
await LlamaCpp.register();
await RunAnywhere.initialize(apiKey: '', baseUrl: '');
```

## Use the right generation method

Dart has no overloading, so there are four:

| You have | Call |
| - | - |
| A prompt | `generate(prompt)` |
| A conversation | `generateChat(messages)` |
| A prompt, streaming | `generateStream(prompt)` |
| A conversation, streaming | `generateChatStream(messages)` |

Passing messages to `generate` does not compile.

## Cancel subscriptions in dispose

```dart theme={"theme":"css-variables"}
@override
void dispose() {
  _subscription?.cancel();
  _session?.close();
  super.dispose();
}
```

A running generation outliving its widget is the most common bug in a Flutter integration, and
it shows up as `setState` after dispose rather than as anything to do with the SDK.

## Check mounted after every await

```dart theme={"theme":"css-variables"}
final result = await RunAnywhere.llm.generate(prompt);
if (!mounted) return;
setState(() => _text = result.text);
```

## Guard deviceId

`RunAnywhere.deviceId` throws before `initialize()`. Read it behind `isReady`.

## Cap output length

`maxOutputTokens` is the biggest lever on latency and battery.

| Use | Reasonable cap |
| - | - |
| Classification, extraction | 20 to 50 |
| Chat reply | 150 to 300 |
| Spoken reply | 100 |

## Unload what you are not using

```dart theme={"theme":"css-variables"}
await RunAnywhere.models.unload('qwen3-0.6b');
await RunAnywhere.models.unloadAll(ModelCategory.MODEL_CATEGORY_LANGUAGE);
```

Holding a language model, a speech model, and a vision model at once on a phone is how you get
`insufficientMemory`.

## Remember the positional optionals

`models.list(filter)` and `models.unloadAll(category)` take positional parameters, not named
ones. `lora.remove(adapterId)` does too.

## One RAG session at a time

The index is single-session. Opening a second while one is open throws. Close the first.

## Rebuild narrowly while streaming

`StreamBuilder` around a large subtree rebuilds all of it on every token. Accumulate into a
`ValueNotifier` and wrap only the text widget in a `ValueListenableBuilder`.

## Pin QHexRT separately

`runanywhere_qhexrt` publishes at 0.20.19 while the rest are at 0.20.27. Aligning the versions
breaks `pub get`.

## Keep the Hugging Face token out of source

```dart theme={"theme":"css-variables"}
RunAnywhere.setHfToken(tokenFromSecureStorage);
```

Use `flutter_secure_storage` or the platform keychain, never a constant in the repo.


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