How to create your Micro-agent using Fetch.ai Technology?


Welcome to this concise tutorial where we will guide you through the process of creating two micro agents that communicate with each other. Our goal is to demonstrate how one agent can request a service while the other agent processes the request based on specific conditions, either carrying out the task or denying it.

In our example, we will focus on a Domain Name Guardian Agent responsible for receiving and processing domain name requests. When a request is made, this Agent will check if the domain name already exists. If it does, the Agent will reject the request and send an error message indicating that the domain name is already taken. On the other hand, if the domain name is available, the Domain Name Guardian Agent will store it in a designated storage location and will confirm the registration by sending a response to another agent, The User Agent.


The User agent will play the role of the requester, continuously making domain name requests every second. Each time a request is made, it will be sent to the Domain Name Guardian Agent.

To create a dynamic interaction between both Agents, we will implement a loop with a 1-second timing procedure. This loop ensures that the User Agent sends different domain name requests every second and awaits the response from the Domain Name Guardian Agent.


This article provides a short introduction into the creation of an agent and covers the fundamental concepts. It serves as a starting point for those who are curious about agent creation using Fetch.ai Technology. However, if you’re interested on going deeper into this topic and exploring more advanced aspects, we highly recommend referring to the comprehensive documentation provided below:

Documentation : here

The documentation offers in-depth insights, detailed explanations, and additional resources that will enable you to expand your understanding and expertise in agent development. Whether you are a beginner or an experienced developer, the documentation will serve as a valuable reference to further explore the fascinating world of agents.

Feel free to explore the documentation and unlock the full potential of agent creation. 

Now that you have an overview of the scenario, let’s dive into the implementation and explore the step-by-step process of creating these micro agents.

Curious on creation an Agent? Let’s get started!

1/ Register to the AgentVerse

First, you need to register and log in to the AgentVerse Website. You can log in with your Gmail address.


2/ Creation of the Domain Name Guardian Agent

Once logged in, you will see the following screen:

Press “Managed Agents” Button and “+ Agent” button.



Let’s give a name to our new Agent:

A new window appears and you can see now the agent address which will be used to communicate with other agents and a “Run button” used to run our Agent.


Let’s add some code!

First let’s create 2 new classes:

  • Request Class named RegisterDomainRequest
  • Response Class RegisterDomainResponse

str : type string

bool : type boolean True or False

Now let’s see how the Agent is dealing with a request :

class RegisterDomainRequest(Model):
    domain_name: str

class RegisterDomainResponse(Model):
    domain_name: str
    success: bool

order_domain_name = Protocol()

@order_domain_name.on_message(model=RegisterDomainRequest, replies={RegisterDomainResponse})
async def handle_order_domain_request(ctx: Context, sender: str, msg: RegisterDomainRequest):

    if ctx.storage.has(str(msg.domain_name)):
        success = False
    else:
        success = True
        ctx.storage.set(str(msg.domain_name), sender)
        ctx.logger.info(f"Domain {msg.domain_name} ordered by {sender}")

    await ctx.send(sender, RegisterDomainResponse(success=success, domain_name=msg.domain_name))

agent.include(order_domain_name)

If the request msg.domain_name already exist into the storage area ctx.storage it will return False (success = False). On the other hand, if the domain name requested by the other Agent is not into the Storage area it will be added :

ctx.storage.set(str(msg.domain_name), sender))

A prompt message is displayed with :

ctx.logger.info(f"Domain {msg.domain_name} ordered by {sender}")

and a message is sent to the Agent that made the request with :

await ctx.send(sender, RegisterDomainResponse(success=success, domain_name=msg.domain_name))

Under the coding area, a prompt which will display the messages of the Agent:

Now our first Agent is ready to Run. If you press on Run button, nothing would happen at the moment. This is because the User Agent, still needs to be created.


3/ Creation of the User Agent

Press “Managed Agents” Button and “+ Agent” button as we did before.

Let’s give a name to our new Agent:


Let’s add some code!

class RegisterDomainRequest(Model):
    domain_name: str

class RegisterDomainResponse(Model):
    domain_name: str
    success: bool

@agent.on_interval(period=1.0)
async def attempt_to_order_domain(ctx: Context):
    existing_domain = ctx.storage.get('domain');
    domain_name = "aviaone"
    i = random.randint(0,10)
    domain_name = domain_name + str(i) + ".fetch"
    ctx.logger.info(f"You try to get the following domain : {domain_name}")
    await ctx.send('agent1qf55hmw0ankuzl6uytmkx4jxu6uz73wvh46xrjgsnadqfm7t6930sxunn7g', RegisterDomainRequest(domain_name=domain_name))

    
@agent.on_message(model=RegisterDomainResponse, replies=set())
async def handle_order_domain_request(ctx: Context, sender: str, msg: RegisterDomainResponse):
    if msg.success:
        ctx.logger.info(f"Success")
    else:
        ctx.logger.info(f"Domain already exist")

The User Agent will execute a request every second with :

@agent.on_interval(period=1.0)

We request for a new domain name for each iteration by using a random number between 0 and 10:

domain_name = "aviaone"

i = random.randint(0,10)

domain_name = domain_name + str(i) + ".fetch"

So an example of a requested domain could be : aviaone1.fetch

Let’s send the request to our other Agent : The Domain Name Guardian Agent

ctx.send('agent1qf55hmw0ankuzl6uytmkx4jxu6uz73wvh46xrjgsnadqfm7t6930sxunn7g', RegisterDomainRequest(domain_name=domain_name))

Note that the Agent Address will be found as following:

If the domain name exists, a prompt message will be displayed :

ctx.logger.info(f"Domain already exist")

4/ Let’s Run our Agents : Results

On left side, the Domain Name Guardian Agent and the right Side the User Agent making requests every seconds. Let’s Press the button Run to both Agents and let’s see what happen:


To reset the storage area, we can do as following:

@agent.on_interval(period=20.0)
async def clear_storage(ctx: Context):
	ctx.logger.info(f"Clear all domain")
	ctx.storage.clear()

Every 20 seconds, the storage area will get empty.

Nos let’s see the result:



It is evident that our agents are functioning effectively, with each agent fulfilling its designated roles. One agent is responsible for making requests, while the other agent handles the processing of those requests and promptly provides a response.

Envision the big potential of this technology when a newly created agent joins the entire network of all interconnected agents, enabling them to communicate, make requests, and respond to each other. This represents a genuine value proposition offered by autonomous agents.

With autonomous agents monitoring multiple facets of your daily life and executing actions based on predetermined parameters and conditions, you can offload a substantial portion of your daily work. This enables you to reclaim valuable time that can be directed towards more significant tasks and priorities.


I trust that you have found this information valuable and that it has inspired you to explore further by experimenting with your own code. For instance, you might be curious about the storage area’s capacity to hold a substantial volume of data. You can put it to the test by attempting to store a larger amount of data and observing the results. Take this opportunity to experiment and see what insights you can get from the outcomes!

For more information about the project, check our Fetch.ai Orange Paper.

Also, Take a look to this Documentation to known more about the library Cosmpy which is a Python library for interacting with Cosmos-based blockchains.

Try it | Love it | Share it