Getting Started with Amazon Bedrock: Your First Model Call
Make your first Amazon Bedrock model call in Python with the Converse API: setup, working code, token costs and fixes for common errors.
Series: Amazon Bedrock (13 articles)
- 2Getting Started with Amazon Bedrock: Your First Model Call This article
From a fresh AWS account to a working response in about fifteen minutes, plus the mistakes that usually eat the first hour.
The first time you open Amazon Bedrock, the hardest part isn't the code. It's working out which of several ways to call a model you should be using, and why your very first request comes back with an error. This guide gets you past both.
By the end you'll have a small Python script that sends a question to a foundation model, prints the answer, and shows you how many tokens it used.
[Add a sentence or two here about your own first run: what you expected, and what tripped you up.]
What you need
- An AWS account, and a budget alert set up before you start (more on that at the end)
- Python 3 on your machine
- About fifteen minutes
Step 1: Choose a region and a model
Bedrock is regional, and not every model is offered in every region. So start by deciding where you'll work and which model you'll try.
Open the model's page in the Amazon Bedrock documentation. Each model has its own card, and it tells you the things you actually need: the model ID, the regions it's available in, which APIs it supports, and its lifecycle status.
That last one matters more than it sounds. Bedrock retires older models on a published schedule, and some cards show a model as legacy with an end-of-life date. Don't build on a model that's about to disappear. Pick one marked active.
Also check whether the model needs any extra approval before you can use it. Some providers ask for a short use-case form the first time. If your first call fails with an access error, this is one of the first things to look at.
Step 2: Set up credentials
The quickest route for learning is a Bedrock API key.
- In the Amazon Bedrock console, generate a long-term API key.
- Save it somewhere safe, and set it as an environment variable:
Bash
export AWS_BEARER_TOKEN_BEDROCK="<your Bedrock API key>"
A word of caution. A long-term key is convenient, but it's a password that doesn't expire on its own. Keep it out of your code and out of Git. For anything beyond experiments, use an IAM role with short-term credentials instead, and give it only the permission to invoke the models it needs. We'll cover that properly in a later article in this series.
If you already use the AWS CLI with a configured profile, you can skip the key and use those credentials, as long as the identity has permission to invoke Bedrock models.
Step 3: Install the SDK
Python
pip install --upgrade boto3
Step 4: Make your first call
Bedrock has more than one way to talk to a model. This article uses the Converse API, because it gives you the same request and response shape across many different models, which makes it easy to swap models later without rewriting your code.
Create a file called
first_call.py :Python
import boto3REGION = "us-east-1" # use the region you choseMODEL_ID = "YOUR_MODEL_ID" # copy this from the model's cardclient = boto3.client("bedrock-runtime", region_name=REGION)response = client.converse(modelId=MODEL_ID,messages=[{"role": "user","content": [{"text": "In two sentences, what is Amazon Bedrock?"}],}],inferenceConfig={"maxTokens": 300, "temperature": 0.3},)answer = response["output"]["message"]["content"][0]["text"]usage = response["usage"]print(answer)print(f"Input tokens: {usage['inputTokens']}, output tokens: {usage['outputTokens']}")
Two things to notice.
First, the client is called
bedrock-runtime . There's also a client called bedrock , and it's easy to grab the wrong one. The plain bedrock client manages things like model lists and settings. The bedrock-runtime client is the one that actually sends prompts.Second,
modelId is pasted straight from the model card. Some models can only be called through an inference profile, and their IDs start with a prefix such as us. or global. . Copy the exact string from the card, prefix included.Step 5: Run it
Bash
python first_call.py
You should see a short answer and a line with the token counts. Those counts are worth a look right away. They're what you're billed on, and they're the first number to check whenever a bill surprises you.
When it doesn't work
These are the errors you're most likely to hit.
AccessDeniedException. Either your credentials don't have permission to invoke the model, or the model needs approval or enabling for your account. Check the permission first, then the model's card.
ValidationException. Usually a wrong model ID or a region where the model isn't offered. Compare your ID and region with the model card, character by character. A missing prefix on an inference profile ID is a classic cause.
ResourceNotFoundException or a model that no longer responds. Check the lifecycle status. The model may have been retired.
ThrottlingException. You're sending requests faster than your account's quota allows. Slow down, retry with a short wait, or ask for a quota increase if you need one.
Nothing happens, or a credentials error. Check that the environment variable is set in the same terminal window where you run the script.
A quick word on cost
You pay per token, both for what you send and for what comes back. A single small test costs next to nothing, but loops, long documents and large outputs add up quickly.
Before you do more than a few experiments:
- Set a monthly budget with an alert in AWS Budgets.
- Keep
maxTokenslow while you're learning.
- Know that for some third-party models, charges appear under the model provider in your bill and in Cost Explorer, not under Amazon Bedrock. It's easy to go looking in the wrong place.
What you've got now
A working call, the token numbers, and a short list of the things that usually go wrong. That's enough to start experimenting.
In the next article we'll compare several models on the same set of prompts, so you can choose one based on results and cost rather than on a guess.
Model availability, IDs, quotas and pricing change often. Check the current Amazon Bedrock documentation before you follow any step here.
Series: Amazon Bedrock (13 articles)
- 2Getting Started with Amazon Bedrock: Your First Model Call This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article