Environment:AI QL Tuui OpenAI Compatible LLM
| Knowledge Sources | |
|---|---|
| Domains | LLM, Streaming, API |
| Last Updated | 2026-10-24 20:38 GMT |
Overview
Any OpenAI-compatible LLM API endpoint supporting chat completions with streaming (SSE), tool calls, and reasoning content extraction.
Description
tuui communicates with LLM providers via the OpenAI-compatible Chat Completions API. The provider URL, model name, API key, content type, and streaming mode are all configurable through llm.json. The application uses the browser fetch API (not axios) to support streaming via ReadableStream. It supports standard Authorization: Bearer headers, x-api-key headers (for Anthropic-style APIs), dynamic JWT token refresh, reasoning effort configuration, and enable_thinking toggles for models like Qwen.
Usage
Use this environment for any implementation that sends chat completion requests to an LLM API. The createCompletion function requires a configured OpenAI-compatible endpoint.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| Network | HTTP/HTTPS access to LLM endpoint | webSecurity: false allows cross-origin
|
| Runtime | Chromium fetch API | Uses browser-native fetch with streaming support
|
Dependencies
NPM Packages
- `jwt-decode` >= 4.0.0 — JWT token expiry checking for dynamic token refresh
Configuration (llm.json)
url— Base URL of the LLM APIpath— API path (e.g.,/v1/chat/completions)model— Model identifier (e.g.,gpt-4o)apiKey— API key for authenticationapiCli— Optional CLI command for dynamic token retrievalstream— Enable SSE streaming (boolean)contentType— Content-Type headerauthorization— Whether to use Authorization header (boolean)authPrefix— Auth prefix (e.g.,Bearer)maxTokensValue/maxTokensPrefix— Max tokens configurationtemperature— Sampling temperaturetopP— Top-p sampling
Credentials
The following configuration values may be required depending on the provider:
apiKey— API key for the LLM provider (stored inllm.json, not in code)apiCli— CLI command that returns a fresh JWT token (for dynamic token providers)
NEVER commit actual API keys to the repository. The llm.json in the repo contains default/placeholder values.
Quick Install
npm install jwt-decode@^4.0.0
Code Evidence
Streaming fetch request from `src/renderer/composables/chatCompletions.ts:267-273`:
const completion = await fetch(
chatbotConfig.url + (chatbotConfig.path ? chatbotConfig.path : ''),
{ ...request, signal: abortController.signal }
)
SSE data line parsing from `src/renderer/composables/chatCompletions.ts:380-393`:
parts
.map((line) => line.trim())
.filter((line) => line.length > 0)
.forEach((line) => {
const pos = line.indexOf(':')
const name = line.substring(0, pos)
if (name !== 'data') return
const content = line.substring(pos + 1).trim()
if (content === '[DONE]') return
parseJson(content, target)
})
JWT expiry check from `src/renderer/composables/chatCompletions.ts:96-106`:
const payload = jwtDecode(chatbotConfig.apiKey)
const currentTime = Math.floor(Date.now() / 1000)
const remaining = payload.exp - currentTime
if (remaining <= 1) {
return await updateToken(chatbotConfig.apiCli)
}
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `401: Unauthorized` | Invalid or expired API key | Check apiKey in llm.json; configure apiCli for auto-refresh
|
| `snackbar.parse-stream-fail` | Response body is not a readable stream | Ensure provider returns SSE format; check stream: true in config
|
| `chat.token-fail` | Token refresh CLI failed | Check apiCli command exists and returns valid token
|
| Redirected URL warning | API returned a redirect | Check the url and path configuration
|
Compatibility Notes
- Anthropic-style APIs: Set
authorization: falseto usex-api-keyheader instead ofAuthorization: Bearer. - Qwen / thinking models:
enable_thinkingandchat_template_kwargsfields are set when configured. - Non-streaming mode: When
stream: false, the entire response is parsed as a single JSON object. - Custom token providers:
apiClispawns a shell command to retrieve fresh tokens; output is read from stdout.