Grok API with Python
Updated:
A working Grok 4.5 integration for Python using the OpenAI SDK and the Grok API Dev base URL. The examples cover a regular request, AsyncOpenAI, streaming, timeouts, and API errors.
Install the Python SDK
Use the current OpenAI package in a virtual environment. Keep the API key outside the source code and pin the tested dependency version in your requirements file or lockfile.
python -m pip install --upgrade openaiexport GROK_API_DEV_KEY="sk-lg-YOUR_API_KEY"The base_url already includes /v1. Do not append /responses when creating the client because the SDK adds the method path itself.
Send the first request
The Responses API accepts a string in input for a minimal text request. The SDK exposes the collected answer through output_text and token accounting through usage.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["GROK_API_DEV_KEY"],
base_url="https://api.llm-gate.tech/v1",
timeout=3600.0,
max_retries=2,
)
response = client.responses.create(
model="grok-4.5",
input="Explain SSE in two sentences.",
)
print(response.output_text)
print(response.usage)Run this code on a server, in a local script, or in a trusted worker. The API key must not be shipped to browser code.
Use AsyncOpenAI
The asynchronous client fits FastAPI, background workers, and services that already use asyncio. Awaiting one request does not limit concurrency on its own, so apply a queue or semaphore when traffic can spike.
import asyncio
import os
from openai import AsyncOpenAI
client = AsyncOpenAI(
api_key=os.environ["GROK_API_DEV_KEY"],
base_url="https://api.llm-gate.tech/v1",
)
async def main() -> None:
response = await client.responses.create(
model="grok-4.5",
input="Hello!",
)
print(response.output_text)
asyncio.run(main())Reuse one client instead of creating a new HTTP connection for every request.
Stream a Grok response
Set stream=True to receive events while the answer is being generated. Append only response.output_text.delta events to the visible text and wait for response.completed before marking the request as finished.
stream = client.responses.create(
model="grok-4.5",
input="Write a short Python example.",
stream=True,
)
for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)A stream can return an error after the connection has opened. Keep the partial text separate from a completed response.
Timeouts, retries, and errors
The OpenAI SDK retries selected temporary failures. Set a finite timeout and max_retries so request behavior stays predictable.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["GROK_API_DEV_KEY"],
base_url="https://api.llm-gate.tech/v1",
max_retries=2,
timeout=3600.0,
)- 401: check GROK_API_DEV_KEY and whether the key is active.
- 404: check the base URL and the grok-4.5 model ID.
- 429: reduce concurrency and retry after a delay.
- Temporary 5xx: use a limited retry with exponential backoff.