|
Voiced by Amazon Polly |
Overview
AI systems are moving from simple chat-based tools to systems capable of performing tasks. This change is mainly due to tool-calling, where an AI model can use external tools such as APIs, databases, or services to fulfill a request.
In this blog post, we will explore the concept of tool-calling and demonstrate how to create agentic AI using two AWS services:
- Amazon Bedrock for reasoning and decision-making
- AWS Lambda for executing actions
Throughout this tutorial, we will discuss architecture, examples, and best practices to help you better understand how such systems are built.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
Previously, the AI models performed tasks such as question answering and text generation. The results depended on the training data, which could sometimes produce wrong or outdated responses.
However, with the tool-calling functionality, AI models can achieve much more now.
Rather than producing a response, they can:
- fetch real-time data
- interact with systems
- execute tasks
For example, if a user asks for stock prices, the model does not guess. It calls a real API, gets the latest data, and then responds.
This makes AI systems more useful in real-world applications like data analysis, automation, and customer support.
AWS provides a strong setup for building such systems using Amazon Bedrock and AWS Lambda.
Tool-Calling
Tool-calling is the ability of an AI model to use external functions when needed.
A simple flow looks like this:
Step-by-Step:
- User sends a query
- The model understands the request
- The model decides which tool is needed
- The tool is executed using Lambda
- The result is returned to the model
- The model generates the final answer
This process can happen multiple times in complex tasks.
How Amazon Bedrock Fits In?
Amazon Bedrock is a fully managed AWS service that provides a wide range of capabilities for accessing advanced AI models without worrying about the underlying infrastructure, the required GPUs, or model deployment.
To put it simply, Bedrock is where the brain of your system lies.
This means you can instead leverage high-quality AI models from Anthropic, Meta, and AWS.
What Amazon Bedrock Actually Does?
When a user sends a request, Amazon Bedrock is responsible for:
- Understanding the user query
- Deciding what to do next
- Selecting the right tool
- Managing multi-step reasoning
- Generating the final response
It also provides Agents, which automatically manage tool-calling workflows and reduce the need for custom logic.
How AWS Lambda Fits In?
AWS Lambda is a serverless compute service that runs your code only when it is needed. In this architecture, AWS Lambda acts as the execution layer.
Whenever Bedrock decides to call a tool, that tool is usually implemented as a Lambda function.
Lambda:
- receives input from the model
- executes logic (API call, DB query, etc.)
- returns structured output
This makes the system scalable and cost-efficient since AWS Lambda runs only when needed.
Multi-Step Agent Workflow
Some tasks require multiple steps.
Example:
“Generate report and email it”
Flow:
- Fetch data
- Process data
- Generate report
- Store file
- Send email
This is handled by the agent using repeated tool calls.
End-to-End Tool Calling Example
This example shows how to connect:
- Amazon Bedrock for reasoning
- AWS Lambda for execution
We define two tools:
- Weather tool
- Web search tool
And implement a simple agent loop that handles tool-calling.
Step 1: Define Tool Schemas
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
tools = [ { "name": "get_weather", "description": "Get current weather for a given city", "input_schema": { "type": "object", "properties": { "city": {"type": "string"} }, "required": ["city"] } }, { "name": "web_search", "description": "Search the web for latest information", "input_schema": { "type": "object", "properties": { "query": {"type": "string"} }, "required": ["query"] } } ] |
Step 2: Create Tool Mapping
|
1 2 3 4 |
tool_map = { "get_weather": "weather_lambda", "web_search": "search_lambda" } |
Step 3: AWS Lambda Invocation Helper
|
1 2 3 4 5 6 7 8 9 10 11 |
import boto3 import json lambda_client = boto3.client("lambda") def call_lambda(function_name, payload): response = lambda_client.invoke( FunctionName=function_name, Payload=json.dumps(payload) ) return json.loads(response["Payload"].read()) |
Step 4: Main Agent Loop
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
bedrock = boto3.client("bedrock-runtime") def run_agent(user_query): messages = [ {"role": "user", "content": user_query} ] for _ in range(5): # safety loop limit response = bedrock.invoke_model( modelId="anthropic.claude-3-sonnet", body=json.dumps({ "messages": messages, "tools": tools }) ) output = json.loads(response["body"].read()) tool_call = output.get("tool_use") # If model wants to call a tool if tool_call: tool_name = tool_call["name"] tool_input = tool_call["input"] lambda_name = tool_map.get(tool_name) if not lambda_name: result = {"error": f"Unknown tool: {tool_name}"} else: result = call_lambda(lambda_name, tool_input) # Add tool call + result back to conversation messages.append(output) messages.append({ "role": "tool", "content": json.dumps(result) }) else: # Final response return output["content"] return "Max iterations reached" |
Real-World Use Cases
- ML Automation
Agents can monitor models, retrain them, and automatically deploy updates.
- Customer Support
Agents can fetch order details, process requests, and respond to users.
- Business Automation
Agents can generate reports, update systems, and send notifications.
Best Practices
- Keep tools small and focused
- Write clear tool descriptions
- Always return structured responses
- Handle errors properly
- Validate all inputs
- Monitor logs and performance
- Control costs by limiting unnecessary calls
Conclusion
This can be done using:
- Amazon Bedrock for intelligence
- AWS Lambda for execution
This represents one of the most common approaches in developing modern AI systems.
Drop a query if you have any questions regarding Tool calling, and we will get back to you quickly.
Empowering organizations to become ‘data driven’ enterprises with our Cloud experts.
- Reduced infrastructure costs
- Timely data-driven decisions
About CloudThat
FAQs
1. Do I need to use Amazon Bedrock Agents, or can I build this manually?
ANS: – You can build tool-calling manually using the Amazon Bedrock API, as shown in this blog. This gives you full control over how tools are selected and executed. However, Amazon Bedrock Agents simplify this process by automatically handling tool routing, session management, and multi-step reasoning. If you want faster development with less code, Agents are a good choice.
2. How does the model decide which tool to call?
ANS: – The model selects tools based on the tool name, description, and input schema you provide. These act like instructions. If the descriptions are clear and specific, the model can reliably choose the correct tool. Poorly written descriptions often lead to incorrect tool selection.
3. Why do we need a mapping layer between tools and AWS Lambda functions?
ANS: – The model only returns a structured tool request (e.g., tool name and inputs). It does not execute the tool itself. Your application must map that tool name to the correct AWS Lambda function and execute it. This mapping layer is essential for connecting model decisions with actual system actions.
WRITTEN BY Aniket Bembale
Login

May 21, 2026
PREV
Comments