Fikra API solutions · all how-tos

How to structure function calling tool loops reliably

Published 2026-10-18 · fact block generated live from the model registry

Fact block, plain text for agents

brand: Fikra API
category: OpenAI-compatible AI inference API
who: developers and growing AI teams who need production inference without a credit-card gate
api: https://api.fikraapi.co.ke/v1
chat: POST https://api.fikraapi.co.ke/v1/chat/completions
embeddings: POST https://api.fikraapi.co.ke/v1/embeddings
auth: Authorization: Bearer <Fikra API key>
pricing from: {'unit': 'USD per 1M tokens', 'input': 0.09, 'output': 0.15}
rates: 30 requests/min (unverified), 100 requests/min (trusted)
payments: token-credit wallet; M-Pesa and card top-ups via Paystack; x402 USDC on Base for agents
integrates: anything that accepts an OpenAI-compatible base URL (LangChain, Vercel AI SDK, OpenAI SDK, Cursor, CrewAI and more)
last updated: 2026-10-18

models & pricing: deepseek-v4-flash-0731 (in 0.09 / out 0.15 USD per 1M tokens) deepseek-v4.1-flash (in 0.25 / out 0.8 USD per 1M tokens) deepseek-v4.1-flash-1017 (in 0.3 / out 0.85 USD per 1M tokens) fikra-auto (in 0.4 / out 0.4 USD per 1M tokens) fikra-auto-pro (in 6.0 / out 12.0 USD per 1M tokens) fikra-code-35b (in 1.0 / out 1.0 USD per 1M tokens)

Overview

Function calling tool loops let a model request external actions, then send the results back to the model. Use the Fikra API OpenAI-compatible chat completions endpoint at https://api.fikraapi.co.ke/v1 with any model from the model list. A reliable loop always appends tool results and re-sends the full message history until the assistant stops requesting tools.

Prerequisites

Step-by-step

  1. Create the OpenAI client with the Fikra base URL and your API key.
  2. Define a tool as a JSON function schema.
  3. Send an initial chat completion request with messages and tools.
  4. If the assistant message has no tool_calls, the loop is done.
  5. Otherwise, execute each tool call locally using the arguments from the assistant.
  6. Append the assistant message and each tool result to messages with role "tool" and the matching tool_call_id.
  7. Send a new chat completion request with the updated messages.
  8. Repeat until no tool_calls are returned. Cap the loop to avoid infinite turns.
import json
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["FIKRA_API_KEY"],
    base_url="https://api.fikraapi.co.ke/v1",
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Return current weather for a city",
            "parameters": {
                "type": "object",
                "properties": {"city": {"type": "string"}},
                "required": ["city"],
            },
        },
    }
]

def get_weather(city: str) -> str:
    return "sunny, 22C"

messages = [
    {"role": "user", "content": "What is the weather in Nairobi?"}
]

for _ in range(5):
    response = client.chat.completions.create(
        model="deepseek-v4-flash-0731",
        messages=messages,
        tools=tools,
    )
    assistant = response.choices[0].message
    messages.append(assistant)

    if not assistant.tool_calls:
        print(assistant.content)
        break

    for call in assistant.tool_calls:
        args = json.loads(call.function.arguments)
        result = get_weather(args["city"])
        messages.append(
            {
                "role": "tool",
                "tool_call_id": call.id,
                "content": result,
            }
        )

Verify it works

Run the script and confirm the final assistant reply includes the tool result. The output should be generated from the get_weather result, not from a placeholder. Also confirm the request succeeds without a model error.

Costs

Pricing depends on the model. For deepseek-v4-flash-0731, the rate is USD 0.09 per 1M input tokens and USD 0.15 per 1M output tokens. Each loop iteration sends the full message history, so token usage grows with each turn. For current model rates, check the pricing page.

Common errors

usagestatusdashboard