How to Use python-A2A to Create and Connect Financial Agents with Google’s Agent-to-Agent (A2A) Protocol

Python A2A is an application of the agent to the agent (A2A) of Google, which enables artificial intelligence agents to communicate with each other using a uniform coordination-the need to integrate allocated between services.
In this tutorial, we will use the decoration-based approach provided by the Python-A2A Library. Simple @factor and @skill Decor, you can identify and behavior of your agent, while the library takes care of processing the protocol and flowing messages.
This method is ideal for building useful factors that focus on tasks quickly without worrying about low -level logic.
Constance stabilization
To start, you will need to install the Python-A2A library, which provides clean stripping and operation of agents that follow the A2A protocol.
Open your station and run:
Create agents
For this tutorial, we will create two agents – one to calculate stock revenues based on investment, rate and time, and the other to control an amount based on inflation over the years.
EMI agent (emi_agent.py)
from python_a2a import A2AServer, skill, agent, run_server, TaskStatus, TaskState
import re
@agent(
name="EMI Calculator Agent",
description="Calculates EMI for a given principal, interest rate, and loan duration",
version="1.0.0"
)
class EMIAgent(A2AServer):
@skill(
name="Calculate EMI",
description="Calculates EMI given principal, annual interest rate, and duration in months",
tags=["emi", "loan", "interest"]
)
def calculate_emi(self, principal: float, annual_rate: float, months: int) -> str:
monthly_rate = annual_rate / (12 * 100)
emi = (principal * monthly_rate * ((1 + monthly_rate) ** months)) / (((1 + monthly_rate) ** months) - 1)
return f"The EMI for a loan of ₹{principal:.0f} at {annual_rate:.2f}% interest for {months} months is ₹{emi:.2f}"
def handle_task(self, task):
input_text = task.message["content"]["text"]
# Extract values from natural language
principal_match = re.search(r"₹?(\d{4,10})", input_text)
rate_match = re.search(r"(\d+(\.\d+)?)\s*%", input_text)
months_match = re.search(r"(\d+)\s*(months|month)", input_text, re.IGNORECASE)
try:
principal = float(principal_match.group(1)) if principal_match else 100000
rate = float(rate_match.group(1)) if rate_match else 10.0
months = int(months_match.group(1)) if months_match else 12
print(f"Inputs → Principal: {principal}, Rate: {rate}, Months: {months}")
emi_text = self.calculate_emi(principal, rate, months)
except Exception as e:
emi_text = f"Sorry, I couldn't parse your input. Error: {e}"
task.artifacts = [{
"parts": [{"type": "text", "text": emi_text}]
}]
task.status = TaskStatus(state=TaskState.COMPLETED)
return task
# Run the server
if __name__ == "__main__":
agent = EMIAgent()
run_server(agent, port=4737)
This EMI calculator is designed using the Python-A2A library and follows the decoration-based approach. On top, we use @factor Decor to determine the name of the agent, description and release it. This records the agent so that he can communicate using the A2A protocol.
Within the chapter, we define one skill using skilDe decor. This skill, called _MI accountThe actual EMI calculates the standard formula. The formula takes three parameters: loan manager, annual interest rate, and loan duration in months. We convert the annual average to a monthly price and use it to the monthly EMI account.
the Handle_task The method is the essence of the agent. It receives the user’s entry message, extracts relevant numbers using simple regular expressions, and transmits them to the Colacone_emi method.
Finally, at the bottom of the file, we launch the agent using Run_server () A job on the port 4737Make it ready to receive A2A protocol messages. This design keeps the agent simple, unilateral, and easy to extend more skills in the future.
Inflation factor (inflation _gent.py)
from python_a2a import A2AServer, skill, agent, run_server, TaskStatus, TaskState
import re
@agent(
name="Inflation Adjusted Amount Agent",
description="Calculates the future value adjusted for inflation",
version="1.0.0"
)
class InflationAgent(A2AServer):
@skill(
name="Inflation Adjustment",
description="Adjusts an amount for inflation over time",
tags=["inflation", "adjustment", "future value"]
)
def handle_input(self, text: str) -> str:
try:
# Extract amount
amount_match = re.search(r"₹?(\d{3,10})", text)
amount = float(amount_match.group(1)) if amount_match else None
# Extract rate (e.g. 6%, 7.5 percent)
rate_match = re.search(r"(\d+(\.\d+)?)\s*(%|percent)", text, re.IGNORECASE)
rate = float(rate_match.group(1)) if rate_match else None
# Extract years (e.g. 5 years)
years_match = re.search(r"(\d+)\s*(years|year)", text, re.IGNORECASE)
years = int(years_match.group(1)) if years_match else None
if amount is not None and rate is not None and years is not None:
adjusted = amount * ((1 + rate / 100) ** years)
return f"₹{amount:.2f} adjusted for {rate:.2f}% inflation over {years} years is ₹{adjusted:.2f}"
return (
"Please provide amount, inflation rate (e.g. 6%) and duration (e.g. 5 years).\n"
"Example: 'What is ₹10000 worth after 5 years at 6% inflation?'"
)
except Exception as e:
return f"Sorry, I couldn't compute that. Error: {e}"
def handle_task(self, task):
text = task.message["content"]["text"]
result = self.handle_input(text)
task.artifacts = [{
"parts": [{"type": "text", "text": result}]
}]
task.status = TaskStatus(state=TaskState.COMPLETED)
return task
if __name__ == "__main__":
agent = InflationAgent()
run_server(agent, port=4747)
This agent helps calculate the amount of the amount specified in the future after the adjustment for inflation. It uses the same structure based on the decoration provided by the Python-A2A Library. the @factor The decoration determines the descriptive data of this agent, and @skill The decor records the main logic under the name “inflation modification”.
the Handle_input The method is where the main treatment occurs. It extracts the amount, the inflation rate, and the number of years from the user inputs using simple regular expressions. If the three values exist, they use the standard future value formula to calculate the average amount that was controlled by inflation:
The modified value = amount x (1 + average/100) ^ years.
If any value is missing, the agent is returning a useful mentor to inform the user of what to be provided, including an example. the Handle_task The function connects everything by taking the user message, transferring it to the skill function, and returning the coordinated result to the user.
Finally, the agent is launched using Run_server () On the port 4747Which makes it ready to deal with A2A inquiries.
Establish the agent network
First, run each of the agents at two separate stations
python inflation_agent.py
Each of these agents displays the end of the API REST (for example http: // Localhost: 4737 for EMI, http: // Localhost: 4747 for inflation) using the A2A protocol. They listen to the tasks received (such as “EMI account for $ 2,00,000 …”)) and respond with text answers.
Now, we will add these agents to our network
from python_a2a import AgentNetwork, A2AClient, AIAgentRouter
# Create an agent network
network = AgentNetwork(name="Economics Calculator")
# Add agents to the network
network.add("EMI", "http://localhost:4737")
network.add("Inflation", "http://localhost:4747")
After that, we will create a router for smart inquiries to the best agent. This is an essential benefit for the A2A protocol – it determines the standard task format so that the agents can be inquired in a uniform, and routers can make smart guidance decisions using LLMS.
router = AIAgentRouter(
llm_client=A2AClient("http://localhost:5000/openai"), # LLM for making routing decisions
agent_network=network
)
Finally, we will inquire about the agents
query = "Calculate EMI for ₹200000 at 5% interest over 18 months."
agent_name, confidence = router.route_query(query)
print(f"Routing to {agent_name} with {confidence:.2f} confidence")
# Get the selected agent and ask the question
agent = network.get_agent(agent_name)
response = agent.ask(query)
print(f"Response: {response}")
query = "What is ₹1500000 worth if inflation is 9% for 10 years?"
agent_name, confidence = router.route_query(query)
print(f"Routing to {agent_name} with {confidence:.2f} confidence")
# Get the selected agent and ask the question
agent = network.get_agent(agent_name)
response = agent.ask(query)
print(f"Response: {response}")

verify Notes notebooks- inflation_janand Network.ipynb and EMI_AGENT.PY. All the credit for this research goes to researchers in this project. Also, do not hesitate to follow us twitter And do not forget to join 100K+ ML Subreddit And subscribe to Our newsletter.

I am a graduate of civil engineering (2022) from Islamic Melia, New Delhi, and I have a strong interest in data science, especially nervous networks and their application in various fields.

Don’t miss more hot News like this! Click here to discover the latest in AI news!
2025-06-17 05:25:00