Mem0 Custom LLM Integration with Fikra API
Prerequisites
- A Fikra API key (
fk_live_...) from the Fikra dashboard. - Python 3.9+ or Node.js 18+.
- The
mem0aipackage installed (pip install mem0aiornpm install mem0ai). - Access to the Fikra API base URL:
https://api.fikraapi.co.ke/v1.
Environment Variable Setup
Set the following environment variables in your shell or .env file:
export FIKRA_API_KEY="fk_live_your_key_here"
export FIKRA_BASE_URL="https://api.fikraapi.co.ke/v1"
Python Example
Mem0 supports custom LLM providers via its LLM configuration. Since Fikra API is OpenAI-compatible, you can use the openai provider type with a custom base URL.
import os
from mem0 import Memory
# Configure Mem0 to use Fikra API via OpenAI-compatible interface
config = {
"llm": {
"provider": "openai",
"config": {
"model": "fikra-flash",
"api_key": os.getenv("FIKRA_API_KEY"),
"base_url": os.getenv("FIKRA_BASE_URL", "https://api.fikraapi.co.ke/v1")
}
},
"embedder": {
"provider": "openai",
"config": {
"model": "text-embedding-3-small",
"api_key": os.getenv("FIKRA_API_KEY"),
"base_url": os.getenv("FIKRA_BASE_URL", "https://api.fikraapi.co.ke/v1")
}
}
}
memory = Memory(config=config)
# Add a memory
memory.add("User prefers dark mode in the app", user_id="user_123")
# Search memories
results = memory.search("What are the user's UI preferences?", user_id="user_123")
print(results)
Note: The embedder configuration above assumes Fikra API supports OpenAI-compatible embedding endpoints. If Fikra API does not expose an embeddings endpoint, you must use a separate embedding provider (e.g., OpenAI, Cohere) for the
embeddersection while keeping thellmsection pointed at Fikra. Check the Fikra model registry for available embedding models.
Streaming Guidance
Fikra API supports streaming via stream: true and Server-Sent Events (SSE). Mem0’s default LLM calls are non-streaming. If you need streaming for direct chat completions (not through Mem0’s memory operations), use the OpenAI SDK directly:
from openai import OpenAI
import os
client = OpenAI(
api_key=os.getenv("FIKRA_API_KEY"),
base_url=os.getenv("FIKRA_BASE_URL", "https://api.fikraapi.co.ke/v1")
)
stream = client.chat.completions.create(
model="fikra-flash",
messages=[{"role": "user", "content": "Hello"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
Common Errors
| HTTP Status | Meaning | Action |
|---|---|---|
| 400 | Bad Request | Verify request body structure, model alias, and message format. Ensure model is a valid Fikra alias (e.g., fikra-flash). |
| 401 | Unauthorized | Check that FIKRA_API_KEY is set correctly and starts with fk_live_. |
| 402 | Payment Required | Your token-credit wallet is empty. Top up via the Fikra dashboard using M-Pesa or card. |
| 429 | Too Many Requests | Rate limit exceeded. Unverified accounts are limited to 30 RPM; trusted accounts to 100 RPM. Implement exponential backoff. |
| 503 | Service Unavailable | Fikra API is temporarily unavailable. Retry with backoff. |
Production Checklist
- [ ] Store
FIKRA_API_KEYin a secrets manager, not in source code. - [ ] Set
FIKRA_BASE_URLexplicitly to avoid defaulting to OpenAI’s endpoint. - [ ] Verify that the model alias (
fikra-flash) is available in the Fikra model registry. - [ ] Implement retry logic with exponential backoff for 429 and 503 responses.
- [ ] Monitor token-credit balance to avoid 402 errors in production.
- [ ] If using embeddings, confirm whether Fikra API provides an embeddings endpoint; otherwise, configure a separate embedding provider.
- [ ] Test streaming separately from Mem0’s memory operations if SSE support is required for user-facing chat.
- [ ] Log request IDs from Fikra API responses for support and debugging.
James Miano
CTO & ML Engineer at Lacesse Ventures. James specializes in low-overhead LLM quantization processes, custom ternary weights architectures, and localized server optimization.
Stop paying for overpriced round-trip latency
Why route queries over Western servers when you can use low-overhead hardware located in Nairobi? Save 87% on your monthly inference spend. No minimum credit limits. M-Pesa ready.