Anthropic SDK
Point the official Anthropic SDK at Gradiated. You only need to change the base URL, API key, and model.
Install#
pip install anthropic
Messages#
from anthropic import Anthropic
import os
client = Anthropic(
base_url="https://api.gradiated.com",
api_key=os.environ["GRADIATED_API_KEY"],
)
message = client.messages.create(
model="YOUR_MODEL",
max_tokens=256,
messages=[
{"role": "user", "content": "Explain inference in one sentence."}
],
extra_body={"service_tier": "default"},
)
print(message.content[0].text)
Replace YOUR_MODEL with a chat model from models and pricing. Decision models accept only SystemOne requests.
The example uses the default service tier. Change to flex for background agents where latency is less important, or fast when you want faster responses and are happy to pay more.
Streaming#
Use the streaming helper to process text as the model generates it.
with client.messages.stream(
model="YOUR_MODEL",
max_tokens=256,
messages=[{"role": "user", "content": "Write one short sentence."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Check models and pricing to confirm that the model supports streaming.
Tool calling#
Send tool definitions with the request. The model can then ask your application to run one of them.
message = client.messages.create(
model="YOUR_MODEL",
max_tokens=256,
messages=[{"role": "user", "content": "What is the weather in Warsaw?"}],
tools=[{
"name": "get_weather",
"description": "Get the current weather for a city.",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}],
)
tool_uses = [block for block in message.content if block.type == "tool_use"]
Your application must validate the tool input and run the function. Send the result in a tool_result block when you continue the conversation.