DEV Community

Cover image for Creating Smart AI Agents with AWS Bedrock

Creating Smart AI Agents with AWS Bedrock

Introduction

Generative AI is transforming industries by automating tasks, improving user experiences, and driving efficiency. One of the most powerful uses of AI is the creation of AI agents — intelligent systems that can perform tasks autonomously based on user input or predefined rules. These agents are being used in everything from customer support to data analysis and even in complex decision-making systems. With the advent of AWS Bedrock, building these AI agents has become more accessible than ever.

In this post, we'll dive into what AI agents are, why AWS Bedrock is the perfect platform to build them and guide you through creating your first AI agent on AWS Bedrock.

Understanding AI Agents

AI agents are autonomous systems that perform tasks or make decisions without continuous human intervention. They can interact with users, learn from data, and even evolve.

Common types of AI agents include:

  • Chatbots: AI agents that simulate human conversations to assist customers.
  • Recommendation Systems: AI agents that suggest products or content based on user behavior.
  • Personal Assistants: Agents that help users by scheduling tasks, setting reminders, and more.
  • Automation Bots: Agents that automate repetitive business processes, such as data entry or transaction processing.

AI agents can either be reactive (responding to user inputs) or proactive (anticipating user needs based on patterns or previous interactions). In this guide, we will focus on building an AI agent that leverages AWS Bedrock for its powerful foundation models.

Why Use AWS Bedrock for AI Agents?

AWS Bedrock provides a suite of tools and services designed to simplify the creation, customization, and deployment of AI models.

Never heard of BedRock before? Read my previous post here.

It offers several benefits when building AI agents:

  • Access to State-of-the-Art Foundation Models: AWS Bedrock provides pre-trained models from top AI research labs like Anthropic, Stability AI, and Cohere. These models serve as the backbone for your AI agents, allowing you to focus on customization rather than training models from scratch.
  • Serverless Architecture: AWS Bedrock takes care of the infrastructure, enabling you to build AI agents without worrying about scaling or resource management. This makes it an ideal solution for businesses of all sizes.
  • Customizable Models: While you can use pre-trained models, AWS Bedrock also allows you to fine-tune these models on your own datasets. This means your AI agent can be trained for specific domains, such as customer support or medical inquiries, enhancing its accuracy and relevance.
  • Seamless Integration with AWS Services: AWS Bedrock integrates well with other AWS services like Lambda, S3, etc, enabling you to create a fully integrated solution that can interact with other applications or databases in your environment.
  • Security and Compliance: With AWS Bedrock, you benefit from the robust security and compliance features provided by AWS, ensuring your AI agents operate securely and meet industry standards.

Components of an AI Agent

An AI Agent has several components. The diagram below illustrates the high-level architecture of how Bedrock agents operate. AWS Bedrock simplifies these components, making it easier for developers to build and deploy AI agents. Let's explore each component. Since it is entirely serverless, we only need to configure each component to make the agent work as we require.

Source: https://docs.aws.amazon.com/bedrock/latest/userguide/agents-how.html<br>

Now, let's take a moment to understand the purpose of each component.

Foundational model

The foundational model serves as the “backbone” of your AI agent. These pre-trained, large-scale machine learning models provide natural language understanding, text generation, and more capabilities. AWS Bedrock offers access to various foundational models from leading providers like Anthropic, HuggingFace, Cohere, Amazon, and many more.

The foundational model provides the base intelligence for the agent, saving you from the need to train models from scratch.

Instructions

Instructions act as the “brain” of the AI agent by defining how the foundational model should behave. These guidelines specify the scope, tone, and task of the agent. Instructions can include:

  • The agent's role (e.g., “You are a customer service assistant.”).
  • Behavior and restrictions (e.g., “Answer politely and avoid technical jargon.”).
  • Desired outputs (e.g., “Provide short, concise answers.”).

Providing clear instructions ensures the foundational model behaves according to your application's requirements.

Action Groups

Action groups enable the AI agent to perform specific tasks or trigger actions beyond text generation. These could include API calls, database queries, or interactions with other systems. For example:

  • Retrieving user details from a database.
  • Sending notifications via an email or messaging API.
  • Performing calculations or processing data inputs.

Action groups allow the AI agent to go beyond static responses and interact dynamically with external systems, making it more functional and capable of solving real-world problems.

In Bedrock, you can integrate a lambda function or OpenAPI schema to define the API operations to invoke an API.

Knowledge bases

A knowledge base provides the AI agent with domain-specific information, enabling it to answer questions or perform tasks that require contextual knowledge. AWS Bedrock allows you to integrate custom datasets (e.g., documents, product catalogs, FAQs) as a knowledge base.

The agent uses this knowledge base to generate more accurate and relevant responses tailored to your organization or use case. Due to this, it is optional to use a knowledge base.

Prompt Templates

Prompt templates provide a structured way to send input to the foundational model. They combine user input with predefined instructions or placeholders to ensure consistency in responses. Amazon Bedrock Agents exposes the default four base prompt templates that are used during the pre-processing, orchestration, knowledge base response generation, and post-processing. By optionally customizing these prompt templates, you can control the quality and format of the AI agent's behavior & outputs.

Creating Your First AI Agent

In this section, we'll walk through the steps to create an AI agent using AWS Bedrock and see how easy it would be to create one. Here, we will create a simple AI Agent that can handle medical appointments to demonstrate its capabilities.

There are several ways that we can create agents on AWS. The easiest way would be to use the conversational builder so that it will interact with us to get the requirements and create the agent according to our responses. However, we will manually configure the agent to gain a better understanding.

1) First, we must request access to a foundation model that can work with agents. Here, we are going to use the Nova Pro model. You can request access to the model via the model access section in the Bedrock configuration on the AWS Management Console.

Request access for base models

2) Then, we can start creating an agent from the builder tools by entering the agent name and description. Since we will create only an agent and not integrate with multiple agents, we are not enabling multi-agent collaboration.

Creating an agent

3) After that, we can further configure the agent via the agent builder. You can attach the foundation model to the agent and provide instructions. The instructor must be clear and precise to ensure the model works accurately.

Use agent builder for further configurations

4) Then, we can add action groups to provide additional capabilities to the agent. Here, we are going to add three lambda functions to,

  • check whether an appointment is available,
  • list available appointments
  • make an appointment

We can create an action group in the console like the one below, which will also create a Lambda function for us. Later, we can update the logic as needed.

Creating action groups

Further, we need to define the required parameters for the lambda function. We must also provide a meaningful description so the AI model can infer the values from the user input and invoke the lambda function.

Define action parameters

We can follow the same method to create all three action groups.

Creating all required action groups

5) Once all three action groups have been created, there should also be three lambda functions corresponding to each. We need to update the logic in each lambda function to perform each action.

Below is a sample lambda handler implementation to check the availability of a given appointment. You can update this implementation to query a database or invoke an API to get the result. This implementation guarantees that appointments will be available except for December 31, 2024. As you can see, we can access the previously defined parameters from the lambda event to implement the logic based on the input values.

Additionally, there is an expected response structure from Lambda functions in Bedrock. The lambda handler should return the correct payload. You can find more information about this structure here.

import json

def lambda_handler(event, context):
    agent = event['agent']
    actionGroup = event['actionGroup']
    function = event['function']
    parameters = event.get('parameters', [])

    print(parameters)

    paramDict = {item['name']: item['value'] for item in parameters}

    print(paramDict['date'])
    print(paramDict['location'])
    print(paramDict['time'])
    print(paramDict['providerName'])

    # Execute your business logic here. For more information, refer to: https://docs.aws.amazon.com/bedrock/latest/userguide/agents-lambda.html
    if(paramDict['date'] == "31/12/2024"):
        print("Unavailable date")
        responseBody =  {
            "TEXT": {
                "body": "This slot is not available"
            }
        }

    else:
        print("Available date")
        responseBody =  {
            "TEXT": {
                "body": "This slot is available"
            }
        }

    action_response = {
        'actionGroup': actionGroup,
        'function': function,
        'functionResponse': {
            'responseBody': responseBody
        }

    }

    dummy_function_response = {'response': action_response, 'messageVersion': event['messageVersion']}
    print("Response: {}".format(dummy_function_response))

    return dummy_function_response
Enter fullscreen mode Exit fullscreen mode

Similarly, we can implement the lambda handler to list appointments by hardcoding some available dates. Again, you can customize this to meet any specific business requirements.

import json

def lambda_handler(event, context):
    agent = event['agent']
    actionGroup = event['actionGroup']
    function = event['function']
    parameters = event.get('parameters', [])

    print("Looking for all available slots")

    # Execute your business logic here. For more information, refer to: https://docs.aws.amazon.com/bedrock/latest/userguide/agents-lambda.html
    responseBody =  {
        "TEXT": {
            "body": "These are the available slots: On 01/01/2025, an appointment is available in Colombo at 9am with Dr. Silva. Another slot is open on 15/01/2025 in Kandy at 11am with Dr. Fernando."
        }
    }

    action_response = {
        'actionGroup': actionGroup,
        'function': function,
        'functionResponse': {
            'responseBody': responseBody
        }

    }

    dummy_function_response = {'response': action_response, 'messageVersion': event['messageVersion']}
    print("Response: {}".format(dummy_function_response))

    return dummy_function_response
Enter fullscreen mode Exit fullscreen mode

Finally, we can implement the lambda function to create the appointment. We will only add a print statement to confirm the behavior in the sample implementation.

import json

def lambda_handler(event, context):
    agent = event['agent']
    actionGroup = event['actionGroup']
    function = event['function']
    parameters = event.get('parameters', [])

    paramDict = {item['name']: item['value'] for item in parameters}

    print("Placed appointment for:", paramDict)

    # Execute your business logic here. For more information, refer to: https://docs.aws.amazon.com/bedrock/latest/userguide/agents-lambda.html
    responseBody =  {
        "TEXT": {
            "body": "Appointment placed successfully"
        }
    }

    action_response = {
        'actionGroup': actionGroup,
        'function': function,
        'functionResponse': {
            'responseBody': responseBody
        }

    }

    dummy_function_response = {'response': action_response, 'messageVersion': event['messageVersion']}
    print("Response: {}".format(dummy_function_response))

    return dummy_function_response
Enter fullscreen mode Exit fullscreen mode

We need to deploy the lambda functions with the new changes we added. We must also save and update the configuration we added on Bedrock to test it.

Here, we will not use a knowledge base or prompt templates to keep it simple. But we can use them to customize the agent even more.

Demo

Here is the output of the agent we created. It asks for missing details if the user hasn't given enough information to proceed. Furthermore, as we defined in the lambda function, the agent says that there are no available appointments on 31/12/2024 and it uses the ListAppointments action to recommend available slots. Finally, it is using the CreateAppointment action to make the appointment.

Demo of the created agent

Here is a video demo of the agent we created. In the CloudWatch logs, we can see all the parameters we entered are captured correctly as well.

Advanced Features

Once you’ve built a basic AI agent, AWS Bedrock offers a range of advanced features that can elevate your solution to the next level. These features enable better customization, optimization, scalability, and integration, making your AI agents smarter and more adaptable for real-world applications.

  • Custom Fine-Tuning for Domain-Specific Tasks: AWS Bedrock allows you to fine-tune foundational models to better align with specific use cases by providing domain-specific datasets (e.g., legal, healthcare, e-commerce). Using this fine-tuned model, you can enhance the agent's performance and accuracy for specialized tasks.
  • Multi-Agent Systems: AWS Bedrock supports the design of multi-agent systems, where multiple AI agents collaborate to perform complex tasks. Each agent can specialize in a specific function and work together by exchanging data and context.
  • Guardrails for Safe and Responsible AI: AWS Bedrock allows you to implement guardrails to ensure AI agents act responsibly and align with business and ethical guidelines. Guardrails can be customized to filter, block, or monitor specific behaviors, enhancing the trustworthiness of AI outputs.
  • Integration with other AWS services: By integrating with other AWS services, you can easily address countless use cases (e.g., integration with step functions to provide AI capabilities to complex workflows).

Conclusion

AI agents powered by AWS Bedrock open up endless possibilities for businesses looking to integrate intelligent automation, enhance user experiences, and drive innovation. By leveraging foundational models, dynamic knowledge bases, guardrails, and multi-agent systems, you can build scalable, secure, and highly customizable solutions.

AWS Bedrock's powerful features make it accessible to teams of all sizes, enabling them to develop sophisticated AI solutions without requiring deep machine-learning expertise. Whether you're automating workflows, building interactive customer support systems, or analyzing complex data, Bedrock provides the tools and flexibility needed to succeed.

As AI continues to evolve, adopting these technologies ensures your business remains competitive and future-ready. Start exploring AWS Bedrock today to unlock the full potential of AI agents and accelerate your journey towards smarter, automated solutions.

Top comments (0)