The Messages API is Anthropic’s native format for interacting with Claude models. Portkey extends it to work with all providers — use the Anthropic SDK pointed at Portkey’s base URL, and switch between providers by changing the model string.
Set a system prompt with the top-level system parameter (not inside messages):
Copy
Ask AI
message = client.messages.create( model="@anthropic-provider/claude-sonnet-4-5-20250514", max_tokens=1024, system="You are a pirate. Always respond in pirate speak.", messages=[{"role": "user", "content": "Say hello."}])
The system parameter also accepts an array of content blocks for prompt caching:
Python
Copy
Ask AI
message = client.messages.create( model="@anthropic-provider/claude-sonnet-4-5-20250514", max_tokens=1024, system=[ {"type": "text", "text": "You are an expert on this topic..."}, {"type": "text", "text": "Here is the reference material...", "cache_control": {"type": "ephemeral"}} ], messages=[{"role": "user", "content": "Summarize the key points"}])
Stream responses with stream=True in the SDK, or the stream parameter in cURL.
Copy
Ask AI
with client.messages.stream( model="@anthropic-provider/claude-sonnet-4-5-20250514", max_tokens=1024, messages=[{"role": "user", "content": "Write a haiku about AI"}]) as stream: for text in stream.text_stream: print(text, end="", flush=True)
Use cache_control on system prompts, messages, and tool definitions to cache frequently-used content.
Copy
Ask AI
message = client.messages.create( model="@anthropic-provider/claude-sonnet-4-5-20250514", max_tokens=1024, system=[{ "type": "text", "text": "You are an expert analyst. Here is a very long reference document...", "cache_control": {"type": "ephemeral"} }], messages=[{"role": "user", "content": "Summarize the key points"}])
Cached content is reused across requests, reducing latency and costs. Cache usage is reflected in the response usage object.
Build conversations by passing the full message history. Messages must alternate between user and assistant roles.
Copy
Ask AI
message = client.messages.create( model="@anthropic-provider/claude-sonnet-4-5-20250514", max_tokens=1024, messages=[ {"role": "user", "content": "My name is Alice."}, {"role": "assistant", "content": "Hello Alice! How can I help you?"}, {"role": "user", "content": "What is my name?"} ])print(message.content[0].text) # "Your name is Alice."