Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including models hosted on AWS Bedrock.With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through Model Catalog.
import Portkey from 'portkey-ai'const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] provider:"@PROVIDER" // Your Bedrock Provider Slug})
from portkey_ai import Portkeyportkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key provider="@PROVIDER" # Replace with Your Bedrock Provider Slug)
If you’re using AWS Security Token Service, you can pass your aws_session_token along with the AI Provider slug:
NodeJS
Python
import Portkey from 'portkey-ai'const portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] provider:"@PROVIDER" // Your Bedrock Provider Slug, aws_session_token: ""})
from portkey_ai import Portkeyportkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key provider="@PROVIDER" # Replace with your Provider Slug for Bedrock, aws_session_token="")
Use the Portkey instance to send requests to Anthropic. You can also override the provider slug directly in the API call if needed.
NodeJS SDK
Python SDK
const chatCompletion = await portkey.chat.completions.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'us.anthropic.claude-3-7-sonnet-20250219-v1:0', max_tokens: 250 // Required field for Anthropic});console.log(chatCompletion.choices);
completion = portkey.chat.completions.create( messages= [{ "role": 'user', "content": 'Say this is a test' }], model= 'us.anthropic.claude-3-7-sonnet-20250219-v1:0', max_tokens=250 # Required field for Anthropic)print(completion.choices)
Portkey’s multimodal Gateway fully supports Bedrock’s vision models anthropic.claude-3-sonnet, anthropic.claude-3-haiku, and anthropic.claude-3-opusFor more info, check out this guide:Vision
The assistants thinking response is returned in the response_chunk.choices[0].delta.content_blocks array, not the response.choices[0].message.content string.
Models like us.anthropic.claude-3-7-sonnet-20250219-v1:0 support extended thinking.
This is similar to openai thinking, but you get the model’s reasoning as it processes the request as well.Note that you will have to set strict_open_ai_compliance=False in the headers to use this feature.
from portkey_ai import Portkey# Initialize the Portkey clientportkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key provider="@PROVIDER", strict_openai_compliance=False)# Create the requestresponse = portkey.chat.completions.create( model="us.anthropic.claude-3-7-sonnet-20250219-v1:0", max_tokens=3000, thinking={ "type": "enabled", "budget_tokens": 2030 }, stream=True, messages=[ { "role": "user", "content": [ { "type": "text", "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" } ] } ])print(response)# in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array# response = portkey.chat.completions.create(# ...same config as above but with stream: true# )# for chunk in response:# if chunk.choices[0].delta:# content_blocks = chunk.choices[0].delta.get("content_blocks")# if content_blocks is not None:# for content_block in content_blocks:# print(content_block)
import Portkey from 'portkey-ai';// Initialize the Portkey clientconst portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key provider:"@PROVIDER", // Add your bedrock's Provider slug from model catalog strictOpenAICompliance: false});// Generate a chat completionasync function getChatCompletionFunctions() { const response = await portkey.chat.completions.create({ model: "us.anthropic.claude-3-7-sonnet-20250219-v1:0", max_tokens: 3000, thinking: { type: "enabled", budget_tokens: 2030 }, stream: true, messages: [ { role: "user", content: [ { type: "text", text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" } ] } ] }); console.log(response); // in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array // const response = await portkey.chat.completions.create({ // ...same config as above but with stream: true // }); // for await (const chunk of response) { // if (chunk.choices[0].delta?.content_blocks) { // for (const contentBlock of chunk.choices[0].delta.content_blocks) { // console.log(contentBlock); // } // } // } }// Call the functiongetChatCompletionFunctions();
import OpenAI from 'openai'; // We're using the v4 SDKimport { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'const openai = new OpenAI({ apiKey: 'dummy', // defaults to process.env["OPENAI_API_KEY"], baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ provider: "@bedrock", // your portkey bedrock provider slug from model catalog apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] strictOpenAICompliance: false })});// Generate a chat completion with streamingasync function getChatCompletionFunctions(){ const response = await openai.chat.completions.create({ model: "us.anthropic.claude-3-7-sonnet-20250219-v1:0", max_tokens: 3000, thinking: { type: "enabled", budget_tokens: 2030 }, stream: true, messages: [ { role: "user", content: [ { type: "text", text: "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" } ] } ], }); console.log(response) // in case of streaming responses you'd have to parse the response_chunk.choices[0].delta.content_blocks array // const response = await openai.chat.completions.create({ // ...same config as above but with stream: true // }); // for await (const chunk of response) { // if (chunk.choices[0].delta?.content_blocks) { // for (const contentBlock of chunk.choices[0].delta.content_blocks) { // console.log(contentBlock); // } // } // }}await getChatCompletionFunctions();
from openai import OpenAIfrom portkey_ai import PORTKEY_GATEWAY_URL, createHeadersopenai = OpenAI( api_key='dummt', base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( provider="@bedrock", # your bedrock provider slug from model catalog api_key="PORTKEY_API_KEY", strict_openai_compliance=False ))response = openai.chat.completions.create( model="us.anthropic.claude-3-7-sonnet-20250219-v1:0", max_tokens=3000, thinking={ "type": "enabled", "budget_tokens": 2030 }, stream=True, messages=[ { "role": "user", "content": [ { "type": "text", "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" } ] } ])print(response)
curl "https://api.portkey.ai/v1/chat/completions" \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ -H "x-portkey-provider: @your-bedrock-provider-slug" \ -H "x-portkey-strict-openai-compliance: false" \ -d '{ "model": "us.anthropic.claude-3-7-sonnet-20250219-v1:0", "max_tokens": 3000, "thinking": { "type": "enabled", "budget_tokens": 2030 }, "stream": true, "messages": [ { "role": "user", "content": [ { "type": "text", "text": "when does the flight from new york to bengaluru land tomorrow, what time, what is its flight number, and what is its baggage belt?" } ] } ] }'
from portkey_ai import Portkey# Initialize the Portkey clientportkey = Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key provider="@PROVIDER", strict_openai_compliance=False)# Create the requestresponse = portkey.chat.completions.create( model="us.anthropic.claude-3-7-sonnet-20250219-v1:0", max_tokens=3000, thinking={ "type": "enabled", "budget_tokens": 2030 }, stream=True, messages=[ { "role": "user", "content": [ { "type": "text", "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" } ] }, { "role": "assistant", "content": [ { "type": "thinking", "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", "signature": "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" } ] }, { "role": "user", "content": "thanks that's good to know, how about to chennai?" } ])print(response)
import Portkey from 'portkey-ai';// Initialize the Portkey clientconst portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key provider:"@PROVIDER", // Add your bedrock's Provider Slug from model catalog strictOpenAICompliance: false});// Generate a chat completionasync function getChatCompletionFunctions() { const response = await portkey.chat.completions.create({ model: "us.anthropic.claude-3-7-sonnet-20250219-v1:0", max_tokens: 3000, thinking: { type: "enabled", budget_tokens: 2030 }, stream: true, messages: [ { role: "user", content: [ { type: "text", text: "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" } ] }, { role: "assistant", content: [ { type: "thinking", thinking: "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" } ] }, { role: "user", content: "thanks that's good to know, how about to chennai?" } ] }); console.log(response); }// Call the functiongetChatCompletionFunctions();
import OpenAI from 'openai'; // We're using the v4 SDKimport { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'const openai = new OpenAI({ apiKey: 'dummy', // we are using provider from portkey baseURL: PORTKEY_GATEWAY_URL, defaultHeaders: createHeaders({ provider: "@bedrock", // Replace with your bedrock provider slug from model catalog apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] strictOpenAICompliance: false })});// Generate a chat completion with streamingasync function getChatCompletionFunctions(){ const response = await openai.chat.completions.create({ model: "us.anthropic.claude-3-7-sonnet-20250219-v1:0", max_tokens: 3000, thinking: { type: "enabled", budget_tokens: 2030 }, stream: true, messages: [ { role: "user", content: [ { type: "text", text: "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" } ] }, { role: "assistant", content: [ { type: "thinking", thinking: "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" } ] }, { role: "user", content: "thanks that's good to know, how about to chennai?" } ], }); console.log(response)}await getChatCompletionFunctions();
from openai import OpenAIfrom portkey_ai import PORTKEY_GATEWAY_URL, createHeadersopenai = OpenAI( api_key='dummy', # we are using provider from portkey base_url=PORTKEY_GATEWAY_URL, default_headers=createHeaders( provider="@bedrock-slug", # Replace with your bedrock provider slug from model catalog api_key="PORTKEY_API_KEY", strict_openai_compliance=False ))response = openai.chat.completions.create( model="us.anthropic.claude-3-7-sonnet-20250219-v1:0", max_tokens=3000, thinking={ "type": "enabled", "budget_tokens": 2030 }, stream=True, messages=[ { "role": "user", "content": [ { "type": "text", "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" } ] }, { "role": "assistant", "content": [ { "type": "thinking", "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", signature: "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" } ] }, { "role": "user", "content": "thanks that's good to know, how about to chennai?" } ])print(response)
curl "https://api.portkey.ai/v1/chat/completions" \ -H "Content-Type: application/json" \ -H "x-portkey-api-key: $PORTKEY_API_KEY" \ -H "x-portkey-provider: @your-bedrock-provider-slug" \ -H "x-portkey-strict-openai-compliance: false" \ -d '{ "model": "us.anthropic.claude-3-7-sonnet-20250219-v1:0", "max_tokens": 3000, "thinking": { "type": "enabled", "budget_tokens": 2030 }, "stream": true, "messages": [ { "role": "user", "content": [ { "type": "text", "text": "when does the flight from baroda to bangalore land tomorrow, what time, what is its flight number, and what is its baggage belt?" } ] }, { "role": "assistant", "content": [ { "type": "thinking", "thinking": "The user is asking several questions about a flight from Baroda (also known as Vadodara) to Bangalore:\n1. When does the flight land tomorrow\n2. What time does it land\n3. What is the flight number\n4. What is the baggage belt number at the arrival airport\n\nTo properly answer these questions, I would need access to airline flight schedules and airport information systems. However, I don't have:\n- Real-time or scheduled flight information\n- Access to airport baggage claim allocation systems\n- Information about specific flights between these cities\n- The ability to look up tomorrow's specific flight schedules\n\nThis question requires current, specific flight information that I don't have access to. Instead of guessing or providing potentially incorrect information, I should explain this limitation and suggest ways the user could find this information.", "signature": "EqoBCkgIARABGAIiQBVA7FBNLRtWarDSy9TAjwtOpcTSYHJ+2GYEoaorq3V+d3eapde04bvEfykD/66xZXjJ5yyqogJ8DEkNMotspRsSDKzuUJ9FKhSNt/3PdxoMaFZuH+1z1aLF8OeQIjCrA1+T2lsErrbgrve6eDWeMvP+1sqVqv/JcIn1jOmuzrPi2tNz5M0oqkOO9txJf7QqEPPw6RG3JLO2h7nV1BMN6wE=" } ] }, { "role": "user", "content": "thanks that's good to know, how about to chennai?" } ] }'
Inference profiles are a resource in Amazon Bedrock that define a model and one or more Regions to which the inference profile can route model invocation requests.To use inference profiles, your IAM role needs to additionally have the following permissions:
You can use Bedrock guardrails directly in your chat completions requests to add content filtering and safety measures. Guardrails help ensure that model responses adhere to your specific safety and content policies.
We recommend using guardrails through the Portkey UI for easier management and configuration.
You can learn more about guardrails here.
To enable guardrails, include the guardrailConfig parameter in your request:
NodeJS SDK
Python SDK
cURL
const chatCompletion = await portkey.chat.completions.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'us.anthropic.claude-3-7-sonnet-20250219-v1:0', max_tokens: 250, guardrailConfig: { guardrailIdentifier: "your-guardrail-id", guardrailVersion: "DRAFT", // or specific version number trace: "enabled" // optional: "enabled" or "disabled" }});
completion = portkey.chat.completions.create( messages=[{ "role": 'user', "content": 'Say this is a test' }], model='us.anthropic.claude-3-7-sonnet-20250219-v1:0', max_tokens=250, guardrail_config={ # Note: snake_case also supported "guardrailIdentifier": "your-guardrail-id", "guardrailVersion": "DRAFT", "trace": "enabled" })
Version of the guardrail ("DRAFT" for the latest draft version, or a specific version number)
trace
string
No
Controls trace generation ("enabled" or "disabled")
Both guardrailConfig (camelCase) and guardrail_config (snake_case) parameter names are supported for compatibility.
When a guardrail is triggered, the response will include a guardrail_intervened stop reason. You can access detailed trace information if tracing is enabled.
Portkey uses the AWS Converse API internally for making chat completions requests.If you need to pass additional input fields or parameters like anthropic_beta, top_k, frequency_penalty etc. that are specific to a model, you can pass it with this key:
Amazon Bedrock service tiers let you choose how Bedrock handles inference capacity for a request. Availability depends on your model, AWS Region, account access, and AWS quotas.Pass service_tier in the request body. Portkey forwards the required Bedrock service tier format for both Chat Completions and Claude /messages requests.
Uses priority capacity when it is available for your model and account.
flex
Uses the Bedrock flex tier when the model supports it.
reserved
Uses reserved capacity when available.
auto, scale, standard_only
Compatibility values from OpenAI or Anthropic clients. Portkey maps these to Bedrock’s default tier.
AWS determines which tiers are available for each model, Region, and account. If a tier is not enabled for your AWS environment, Bedrock may reject the request.
Returns Bedrock’s tier value when Bedrock includes it in the response, such as default, flex, priority, or reserved.
/messages
usage.service_tier
Returns standard or priority, following Anthropic’s response shape. Portkey maps Bedrock default and flex to standard, and priority and reserved to priority.
The examples below use priority. Replace it with any tier supported for your model and AWS account.
const chatCompletion = await portkey.chat.completions.create({ messages: [{ role: "user", content: "Say this is a test" }], model: "moonshot.kimi-k2-thinking", max_tokens: 250, service_tier: "priority"})
completion = portkey.chat.completions.create( messages=[{ "role": "user", "content": "Say this is a test" }], model="moonshot.kimi-k2-thinking", max_tokens=250, service_tier="priority")
You can manage all prompts to AWS bedrock in the Prompt Library. All the current models of Anthropic are supported and you can easily start testing different prompts.Once you’re ready with your prompt, you can use the portkey.prompts.completions.create interface to use the prompt in your application.
Using AWS PrivateLink for Bedrock [Self Hosted Enterprise]
Though using assumed role is in itself enough for enterprise security. You can additional configure AWS PrivateLink for Bedrock to ensure that your requests are not traversed outside your VPC.
Create a private link between the VPC you’ve deployed Portkey and AWS Bedrock (the endpoint is in most cases https://bedrock.{your_region}.amazonaws.com).
When configuring your integration on portkey, simply configure the custom host option to point to your VPC endpoint for the private link.