Native SDKs
OpenAI SDK
Use TokenLab with official OpenAI SDKs and OpenAI-compatible clients
Overview
Point an OpenAI SDK client to https://api.tokenlab.sh/v1 and use a TokenLab API key. Keep the API method your application already uses, and confirm the selected model supports it through accepted_request_formats.
Python, JavaScript, and Go have official OpenAI SDKs. PHP works well with OpenAI-compatible community clients, but it is not an official OpenAI SDK.
Installation
pip install openaiUse POST /v1/responses for Responses input items, tools, or continuation. Optional fields depend on the selected model.
Configure the Client
from openai import OpenAI
client = OpenAI(
api_key="sk-your-tokenlab-key",
base_url="https://api.tokenlab.sh/v1",
)Recommended: Chat Completions
response = client.chat.completions.create(
model="gpt-5.6-terra",
messages=[{"role": "user", "content": "Reply with: Connected to TokenLab."}]
)
print(response.choices[0].message.content)Advanced: Responses API
Use this path only when your tool or workflow explicitly depends on OpenAI Responses semantics.
Streaming with Responses
stream = client.responses.create(
model="gpt-5.6-terra",
input="Write a short poem about coding.",
stream=True,
)
for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, end="")Tools / Function Calling
response = client.responses.create(
model="gpt-5.6-terra",
input="What's the weather in Tokyo?",
tools=[{
"type": "function",
"name": "get_weather",
"description": "Get weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}]
)
for item in response.output:
if item.type == "function_call":
print(item.name)
print(item.arguments)Vision with Responses
response = client.responses.create(
model="gpt-5.6-terra",
input=[{
"role": "user",
"content": [
{"type": "input_text", "text": "What's in this image?"},
{"type": "input_image", "image_url": "https://example.com/image.jpg"}
]
}]
)
print(response.output_text)Embeddings
response = client.embeddings.create(
model="text-embedding-3-small",
input="Hello world"
)
print(response.data[0].embedding[:5])Chat Completions
Chat Completions is the default OpenAI-compatible path for TokenLab:
response = client.chat.completions.create(
model="gpt-5.6-terra",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
print(response.choices[0].message.content)