TokenLab

Frameworks & Platforms

AutoGen

Use TokenLab models with Microsoft AutoGen through OpenAI-compatible clients

Overview

AutoGen can call TokenLab with OpenAIChatCompletionClient. Configure the TokenLab Base URL, API key, and model ID in the client.

Installation

pip install -U autogen-agentchat "autogen-ext[openai]" pyyaml

Environment

export TOKENLAB_API_KEY="sk-your-tokenlab-key"

YAML Model Config

Use TokenLab's /v1 endpoint as base_url:

Save this configuration as tokenlab-model.yaml:

provider: autogen_ext.models.openai.OpenAIChatCompletionClient
config:
  model: claude-sonnet-5
  base_url: https://api.tokenlab.sh/v1
  model_info:
    function_calling: true
    json_output: true
    structured_output: false
    vision: false
    family: claude

Load the YAML and read the API key from the environment. YAML does not expand environment variables automatically:

import os
import yaml

from autogen_agentchat.agents import AssistantAgent
from autogen_core.models import ChatCompletionClient

with open("tokenlab-model.yaml", encoding="utf-8") as config_file:
    config = yaml.safe_load(config_file)

config["config"]["api_key"] = os.environ["TOKENLAB_API_KEY"]
model_client = ChatCompletionClient.load_component(config)
agent = AssistantAgent("assistant", model_client=model_client)

Python Client

import os

from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient

model_client = OpenAIChatCompletionClient(
    model="claude-sonnet-5",
    base_url="https://api.tokenlab.sh/v1",
    api_key=os.environ["TOKENLAB_API_KEY"],
    model_info={
        "function_calling": True,
        "json_output": True,
        "structured_output": False,
        "vision": False,
        "family": "claude",
    },
)

agent = AssistantAgent("assistant", model_client=model_client)

Run your first request

import asyncio

async def main():
    try:
        result = await agent.run(task="Reply with: Connected to TokenLab.")
        print(result.messages[-1].content)
    finally:
        await model_client.close()

asyncio.run(main())

Endpoint Notes

AutoGen's OpenAI extension expects the OpenAI-compatible request shape. For provider-native features such as Anthropic Messages or Gemini generateContent, use TokenLab's native endpoints from a client that supports those protocols directly.

On this page