sharpbyte.dev
← AI agents
AI agents · topic 3 of 16

Building blocks of AI Agents

Role, focus, tools (including custom tools and MCP), cooperation, guardrails, and memory—through the deck’s CrewAI-oriented examples (PDF 182–196; 182 shared with topic 2).

Building blocks of AI Agents

Building blocks of AI Agents AI agents are designed to reason, plan, and take action autonomously. However,

to be effective, they must be built with certain key principles in mind. There are six essential building blocks that make AI agents more reliable, intelligent, and useful in real-world applications: 1. Role-playing 2. Focus 3. Tools 4. Cooperation 5. Guardrails 6. Memory Let’s explore each of these concepts and understand why they are fundamental to building great AI agents.

1) Role-playing

One of the simplest ways to boost an agent’s performance is by giving it a clear, specific role.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

A generic AI assistant may give vague answers. But define it as a “Senior contract lawyer,” and it responds with legal precision and context. Why? Because role assignment shapes the agent’s reasoning and retrieval process. The more specific the role, the sharper and more relevant the output.

2) Focus/Tasks

Focus is key to reducing hallucinations and improving accuracy.

Giving an agent too many tasks or too much data doesn’t help - it hurts.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Overloading leads to confusion, inconsistency, and poor results. For example, a marketing agent should stick to messaging, tone, and audience not pricing or market analysis. Instead of trying to make one agent do everything, a better approach is to use multiple agents, each with a specific and narrow focus. Specialized agents perform better - every time.

3) Tools

Agents get smarter when they can use the right tools. But more tools ≠ better results.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

For example, an AI research agent could benefit from:

● A web search tool for retrieving recent publications.

● A summarization model for condensing long research papers.

● A citation manager to properly format references.

But if you add unnecessary tools—like a speech-to-text module or a code execution environment—it could confuse the agent and reduce efficiency. #3.1) Custom tools While LLM-powered agents are great at reasoning and generating responses, they lack direct access to real-time information, external systems, and specialized computations. Tools allow the Agent to:

● Search the web for real-time data.

● Retrieve structured information from APIs and databases.

● Execute code to perform calculations or data transformations.

● Analyze images, PDFs, and documents beyond just text inputs.

CrewAI supports several tools that you can integrate with Agents, as depicted below:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

However, you may need to build custom tools at times. In this example, we're building a real-time currency conversion tool inside CrewAI. Instead of making an LLM guess exchange rates, we integrate a custom tool that fetches live exchange rates from an external API and provides some insights. Below, let's look at how you can build one for your custom needs in the CrewAI framework. Firstly, make sure the tools package is installed:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

You would also need an API key from here: https://www.exchangerate-api.com/ (it's free). Specify it in the .env file as shown below:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Once that's done, we start with some standard import statements:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Next, we define the input fields the tool expects using Pydantic.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Now, we define the CurrencyConverterTool by inheriting from BaseTool:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Every tool class should have the _run method which we will execute whenever the Agents wants to make use of it. For our use case, we implement it as follows:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

In the above code, we fetch live exchange rates using an API request. We also handle errors if the request fails or the currency code is invalid. Now, we define an agent that uses the tool for real-time currency analysis and attach our CurrencyConverterTool, allowing the agent to call it directly if needed:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

We assign a task to the currency_analyst agent.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Finally, we create a Crew, assign the agent to the task, and execute it.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Printing the response, we get the following output:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Works as expected! #3.2) Custom tools via MCP Now, let’s take it a step further.

Instead of embedding the tool directly in every Crew, we’ll expose it as a reusable MCP tool - making it accessible across multiple agents and flows via a simple server. First, install the required packages:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

We’ll continue using ExchangeRate-API in our .env file:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

We’ll now write a lightweight server.py script that exposes the currency converter tool. We start with the standard imports:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Now, we load environment variables and initialize the server:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Next, we define the tool logic with @mcp.tool():

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

This function takes three inputs - amount, source currency, and target currency and returns the converted result using the real-time exchange rate API. To make the tool accessible, we need to run the MCP server. Add this at the end of your script:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

This starts the server and exposes your convert_currency tool at: http://localhost:8081/sse. Now any CrewAI agent can connect to it using MCPServerAdapter. Let’s now consume this tool from within a CrewAI agent. First, we import the required CrewAI classes. We’ll use Agent, Task, and Crew from CrewAI, and MCPServerAdapter to connect to our tool server.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Next, we connect to the MCP tool server. Define the server parameters to connect to your running tool (from server.py).

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Now, we use the discovered MCP tool in an agent: This agent is assigned the convert_currency tool from the remote server. It can now call the tool just like a locally defined one.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

We give the agent a task description:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Finally, we create the Crew, pass in the inputs and run it:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Printing the result, we get the following output:

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

4) Cooperation

Multi-agent systems work best when agents collaborate and exchange feedback. Instead of one agent doing everything, a team of specialized agents can split tasks and improve each other’s outputs.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Consider an AI-powered financial analysis system:

● One agent gathers data

● another assesses risk,

● a third builds strategy,

● and a fourth writes the report

Collaboration leads to smarter, more accurate results. The best practice is to enable agent collaboration by designing workflows where agents can exchange insights and refine their responses together.

5) Guardrails

Agents are powerful but without constraints, they can go off track. They might

hallucinate, loop endlessly, or make bad calls. Guardrails ensure that agents stay on track and maintain quality standards.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Examples of useful guardrails include:

● Limiting tool usage: Prevent an agent from overusing APIs or generating irrelevant queries.

● Setting validation checkpoints: Ensure outputs meet predefined criteria before moving to the next step.

● Establishing fallback mechanisms: If an agent fails to complete a task, another agent or human reviewer can intervene. For example, an AI-powered legal assistant should avoid outdated laws or false claims - guardrails ensure that.

6) Memory

Finally, we have memory, which is one of the most critical components of AI agents. Without memory, an agent would start fresh every time, losing all context from previous interactions. With memory, agents can improve over time, remember past actions, and create more cohesive responses.

Illustration from the AI Agents chapter of the course deck.
Illustration from the AI Agents chapter of the course deck.

Different types of memory in AI agents include:

● Short-term memory – Exists only during execution (e.g., recalling recent conversation history).

● Long-term memory – Persists after execution (e.g., remembering user preferences over multiple interactions).

● Entity memory – Stores information about key subjects discussed (e.g., tracking customer details in a CRM agent). For example, in an AI-powered tutoring system, memory allows the agent to recall past lessons, tailor feedback, and avoid repetition.

Key takeaways

  • Narrow roles and scoped tasks outperform one overloaded generalist agent.
  • Tools and guardrails turn language into reliable action in real systems.