By Fikra DevRel • 4 min read

AutoGen Custom Model Setup for Fikra API

AutoGen Custom Model Setup for Fikra API

Prerequisites

  • Python 3.9+ installed.
  • autogen-agentchat and openai packages 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 model parameter matches a listed Fikra alias (e.g., fikra-flash).
  • 401 Unauthorized: Missing or invalid API key. Ensure FIKRA_API_KEY is correctly set and starts with fk_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

  1. API Key Management: Store FIKRA_API_KEY in a secure environment variable or secret manager.
  2. Rate Limiting: Implement client-side throttling to stay within 30 RPM (unverified) or 100 RPM (trusted) limits.
  3. Error Handling: Add retry logic with exponential backoff for 429 and 503 errors.
  4. Monitoring: Log request latency and error codes for observability.
  5. Model Selection: Use fikra-flash for low-latency tasks; check the model registry for other aliases like fikra-pro-20b if higher quality is needed.
  6. Billing: Monitor token credit usage in the Fikra dashboard to avoid 402 errors.

JM
// The Founder

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.

usagestatusdashboard