Skip to content

LlamaIndex

LlamaIndex’s OpenAI LLM class accepts an api_base override. Point it at MirAPI and the whole indexing and querying stack (LLM, embeddings, and agents) runs on your key.

Element Value
Base URL https://api.mirapi.ai/v1 (api_base)
API key MIRAPI_API_KEY
Model deepseek-chat

api_base must end in /v1: LlamaIndex appends the path (/chat/completions, /embeddings) itself.

Terminal window
pip install llama-index llama-index-llms-openai
import os
from llama_index.core import Settings
from llama_index.llms.openai import OpenAI
Settings.llm = OpenAI(
model="deepseek-chat",
api_key=os.environ["MIRAPI_API_KEY"],
api_base="https://api.mirapi.ai/v1",
)

LlamaIndex passes api_base through to the OpenAI SDK’s base_url, so api_base is the parameter to set. The api_key here is any MirAPI key from the Platform. MirAPI accepts the standard OpenAI Authorization: Bearer scheme, and the OpenAI class sends it automatically.

response = Settings.llm.complete("Reply with exactly: connected to MirAPI")
print(response.text)

The output should contain connected to MirAPI. If it does not, start with Errors and troubleshooting below.

Embeddings use a separate model class and a dedicated endpoint; a chat model will not work here.

from llama_index.embeddings.openai import OpenAIEmbedding
Settings.embed_model = OpenAIEmbedding(
model="<embedding-model-id>",
api_key=os.environ["MIRAPI_API_KEY"],
api_base="https://api.mirapi.ai/v1",
)

Pick an embedding model ID from the catalog and set Settings.embed_model once. Every VectorStoreIndex you build then embeds through /v1/embeddings. Prompt caching applies to the chat model, not to embeddings. When the LLM reuses a large system prompt or index context, cache reads cost less than ordinary input, and hits are visible in usage.prompt_tokens_details.cached_tokens.

MirAPI supports the OpenAI tools / tool_choice format, so LlamaIndex FunctionTool and agents that emit tool calls work without extra configuration. The model must advertise tool calling in the catalog, and deepseek-chat does. See the tool-calling agent tutorial for a full example.

Streaming, reasoning, and structured outputs

Section titled “Streaming, reasoning, and structured outputs”
  • Streaming works through the standard stream_complete / stream_chat methods. MirAPI streams SSE data: lines ending with data: [DONE], and reasoning tokens arrive before the final answer.
  • For reasoning models, set reasoning_effort to control how much the model reasons; reasoning tokens are billed as output.
  • For typed extraction, LlamaIndex’s structured_predict / as_structured_llm map to the OpenAI-compatible response_format (JSON mode or json_schema); the model must advertise JSON output in the catalog. See Structured outputs.
Code Meaning What to do
401 Key missing, invalid, or unknown Check MIRAPI_API_KEY and that it comes from the Platform.
403 Key valid but rejected: empty balance, model outside the whitelist, quota, or IP allowlist Fix the cause (top up, add the model, raise the quota) and retry.
413 Payload too large Reduce the input size.
429 Rate limited Retry with jittered exponential backoff (1s → 2s → 4s, cap ~30s). There is no Retry-After header and the gateway does not retry for you.
500 Upstream failure Idempotent requests are safe to retry.

Every error message ends with a request ID; include it in any support ticket.

MirAPI bills text and multimodal chat per token, priced in USD per 1M tokens with separate input, output, and cache-read prices. Charges come out of a prepaid balance that never expires, and there is no subscription. Reasoning tokens count as output. When the balance runs out, the API returns 403; the same key works again immediately after you top up. Reconcile spend in the Platform billing log, GET /api/usage/token (account-level summary), or GET /api/log/token (per-request detail).

  • api_base must include /v1. LlamaIndex appends /chat/completions and /embeddings.
  • Embeddings are a separate class. Passing a chat model ID to OpenAIEmbedding (or vice versa) fails.
  • A valid key that returns 403 usually means an empty balance or a model outside the key’s whitelist; top up or fix the key and retry.
  • 429 has no Retry-After. Implement your own backoff; the gateway will not retry on your behalf.