AutoGen Custom Model Setup for Fikra API
Prerequisites
- Python 3.9+ installed.
autogen-agentchatandopenaipackages installed (pip install autogen-agentchat openai).- A valid Fikra API key (
fk_live_...) from the Fikra dashboard. - Access to the Fikra API base URL:
https://api.fikraapi.co.ke/v1.
Environment Variable Setup
Store your API key in an environment variable to avoid hardcoding secrets:
export FIKRA_API_KEY="fk_live_your_key_here"
Python Example: Configuring AutoGen with Fikra API
AutoGen supports custom LLM configurations via the OpenAI client. Since Fikra API is OpenAI-compatible, you can configure it by specifying the base_url and api_key.
import os
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
# Initialize the Fikra API client
client = OpenAIChatCompletionClient(
model="fikra-flash",
api_key=os.environ["FIKRA_API_KEY"],
base_url="https://api.fikraapi.co.ke/v1"
)
# Create an agent using the Fikra model
agent = AssistantAgent(
name="fikra_agent",
model_client=client,
system_message="You are a helpful assistant powered by Fikra API."
)
# Example usage
response = agent.run(task="Explain the concept of API rate limiting in simple terms.")
print(response)
Streaming Guidance
Fikra API supports streaming via Server-Sent Events (SSE) when stream: true is set. In AutoGen, streaming is handled internally by the OpenAIChatCompletionClient. To enable streaming, ensure your client configuration allows it. For direct HTTP calls, you would set "stream": true in the request body and parse the SSE stream.
Example of a direct streaming request (for reference):
import requests
import os
response = requests.post(
"https://api.fikraapi.co.ke/v1/chat/completions",
headers={
"Authorization": f"Bearer {os.environ['FIKRA_API_KEY']}",
"Content-Type": "application/json"
},
json={
"model": "fikra-flash",
"messages": [{"role": "user", "content": "Hello"}],
"stream": True
},
stream=True
)
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
Common Errors
- 400 Bad Request: Invalid request format, missing required fields, or unsupported model alias. Verify the
modelparameter matches a listed Fikra alias (e.g.,fikra-flash). - 401 Unauthorized: Missing or invalid API key. Ensure
FIKRA_API_KEYis correctly set and starts withfk_live_. - 402 Payment Required: Insufficient token credits in your Fikra wallet. Top up via the Fikra dashboard using Paystack (M-Pesa or cards accepted).
- 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: Temporary server issue. Retry with backoff. Check the Fikra dashboard for status updates.
Production Checklist
- API Key Management: Store
FIKRA_API_KEYin a secure environment variable or secret manager. - Rate Limiting: Implement client-side throttling to stay within 30 RPM (unverified) or 100 RPM (trusted) limits.
- Error Handling: Add retry logic with exponential backoff for 429 and 503 errors.
- Monitoring: Log request latency and error codes for observability.
- Model Selection: Use
fikra-flashfor low-latency tasks; check the model registry for other aliases likefikra-pro-20bif higher quality is needed. - Billing: Monitor token credit usage in the Fikra dashboard to avoid 402 errors.
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.