
The Hands of AI: Tool Integration and Stateful Decision Systems
Give your AI the ability to act. Learn how to define API schemas for tool integration and manage state across complex, multi-turn agent conversations.
Giving the AI a Grip
In the previous lesson, we designed the "Brain" of the agent. Now, we must give it "Hands." In Generative AI, these hands are called Tools (or Action Groups in the context of Amazon Bedrock). A tool allows the model to reach outside its neural network and interact with the real world—calculating math, search a database, or sending an email.
In this lesson, we will master Tool Definition, Function Calling, and State Management.
1. What is a Tool? (Function Calling)
A tool is simply a piece of code (often a Lambda function) that the model can choose to execute. The model doesn't "run" the function itself; instead:
- The model says: "I want to run
get_weatherfor city 'London'." - Your application (or AWS Bedrock) executes the code and gets the result.
- Your application sends the result back to the model.
2. Defining Tools with JSON Schema
To use a tool, the model must understand the "Interface." We define this using OpenAPI or JSON Schema.
{
"name": "get_stock_price",
"description": "Get the current price for a specific stock ticker.",
"parameters": {
"ticker": {
"type": "string",
"description": "The stock symbol (e.g. AMZN, AAPL)."
}
},
"required": ["ticker"]
}
Pro Tip: The description is the most important part. If you write a bad description, the model won't know when to use the tool.
3. Bedrock Action Groups
In Amazon Bedrock Agents, tools are organized into Action Groups.
- The Trigger: You provide an OpenAPI (Swagger) schema.
- The Executor: You provide an AWS Lambda function.
- The Handshake: When the model wants to act, Bedrock automatically reformats the request into a JSON payload and invokes your Lambda.
graph LR
A[Bedrock Agent] -->|Tool Selection| B[Bedrock Action Group]
B -->|JSON Payload| C[AWS Lambda]
C -->|API Call| D[External Service: e.g. Salesforce]
D -->|Data| C
C -->|Response| B
B -->|Observation| A
4. Stateful Decision Systems
A "Stateful" agent remembers what happened in previous turns.
- Session State: Using
sessionIdin Bedrock ensures the model carries the context of the conversation. - Variables: You can pass "Session Attributes" from your code to the agent. For example, you can tell the agent
user_tier = "Platinum"so it knows which tools are allowed for this specific user.
5. Handling Tool Failures
What if the database is down? As a Professional Developer, you must handle the "Observation" of a failure.
- The Observation: Instead of crashing, your code should return: "Error: The database reached a timeout. Please try again in 5 minutes."
- The Model's Action: The model will read this error and say to the user: "I'm sorry, I'm having trouble reaching our inventory records right now. Would you like me to try a different task?"
6. Code Example: A Pro-Lambda for Action Groups
def lambda_handler(event, context):
# Extracts parameters passed by the Bedrock Agent
action_group = event['actionGroup']
function_name = event['function']
parameters = event['parameters'] # e.g. [{'name': 'ticker', 'value': 'AMZN'}]
if function_name == 'get_stock_price':
ticker = parameters[0]['value']
# Mock API logic
price = 180.00
response_body = {
"TEXT": f"The price of {ticker} is ${price}."
}
# Format required by Bedrock Action Groups
response = {
'actionGroup': action_group,
'function': function_name,
'functionResponse': {
'responseBody': response_body
}
}
return response
Knowledge Check: Test Your Tool Knowledge
?Knowledge Check
When defining an Action Group for an Amazon Bedrock Agent, which component is responsible for telling the model 'What the tool does' and 'Which parameters it requires'?
Summary
Tools turn an AI from a "Thinker" into a "Doer." By mastering Action Groups and Lambda integration, you can build agents that actually run your business processes. In the final lesson of Domain 2, we will look at Monitoring Agent Execution.
Next Lesson: The Guardian: Monitoring Agent Execution