This project demonstrates a simple Agentic AI workflow built with Python.
The agent can select and use different tools based on the user's request.
- Tool Registry
@toolDecorator- Calculator Tool
- Weather Tool
- Word Count Tool
- Simple Keyword-Based Routing
- Argument Extraction
- JSON Function Calls
- Tool Execution
- Retry Mechanism
- Conversation History
- Execution Logging
- Reason β Act β Observe Workflow
The calculator tool evaluates mathematical expressions.
Example request:
Calculate 5 + 4
The agent detects the mathematical expression and selects the calculator tool.
The weather tool returns fake weather information for testing purposes.
Example request:
What is the weather in Lahore?
The agent extracts the city name and calls the get_weather tool.
The weather tool also randomly raises a temporary error to demonstrate the retry mechanism.
The word count tool counts the total number of words in the provided text.
Example request:
How many words are in this text?
The agent selects the words_count tool.
The project uses a dictionary called tool_registery to store the available tools.
Each tool is registered using the @tool decorator.
The registry maps:
Tool Name β Function
For example:
calculator β calculator()
get_weather β get_weather()
words_count β words_count()
This allows the agent to find and execute tools dynamically.
The agent uses a simple rule-based router to decide which tool should be used.
User Message
β
route()
β
βββββββββββββββββ
β Weather? β β get_weather
β Word count? β β words_count
β Math expression? β β calculator
βββββββββββββββββ
For example:
| User Request | Selected Tool |
|---|---|
| Calculate 5 + 4 | calculator |
| Weather in Lahore | get_weather |
| How many words? | words_count |
After selecting a tool, the agent extracts the required arguments from the user's message.
User:
Calculate 5 + 4
Arguments:
{
"expression": "5 + 4"
}
User:
What is the weather in Multan?
Arguments:
{
"city": "Multan"
}
Arguments:
{
"text": "user message"
}
The agent represents the selected tool and its arguments as a JSON-style function call.
Example:
{
"tool": "calculator",
"arguments": {
"expression": "5 + 4"
}
}For a weather request:
{
"tool": "get_weather",
"arguments": {
"city": "Multan"
}
}This makes the tool decision visible before execution.
After creating the function call, the agent executes the selected tool.
The execution flow is:
Tool Name
β
Find Function in Tool Registry
β
Pass Arguments
β
Execute Function
β
Get Result
The selected function receives its arguments and returns a result.
The agent includes a retry mechanism for failed tool executions.
Each tool can be attempted up to 3 times.
Attempt 1
β
Error
β
Attempt 2
β
Error
β
Attempt 3
β
Success / Final Failure
The weather tool intentionally generates temporary errors so that this retry system can be tested.
If the tool still fails after 3 attempts, the agent returns a failure message.
After successful tool execution, the returned value becomes the observation.
Example:
Observation:
9
The agent then uses this result to create the final answer.
The agent creates a final response from the tool result.
Example:
Final Answer:
Result: 9
For weather:
Final Answer:
Result: 38Β°C, Sunny
The agent stores each turn in:
self.history
Each history record contains:
- User message
- Tool call
- Result
- Final answer
Example:
{
"user_message": "Calculate 5 + 4",
"tool_call": {
"tool": "calculator",
"arguments": {
"expression": "5 + 4"
}
},
"result": 9,
"final_answer": "Result: 9"
}Every agent turn is saved to:
agent-log.txt
The log records the execution history of the agent.
It contains:
- User message
- Selected tool
- Tool arguments
- Tool result
- Final answer
The complete agent workflow is:
User Message
β
Reason
β
route()
β
Select Tool
β
arguments()
β
Create JSON Function Call
β
Act
β
execute_tool()
β
Try / Retry up to 3 times
β
Observe
β
Result
β
Final Answer
β
Conversation History
β
agent-log.txt
Calculate 5 + 4
Expected flow:
User Message
β
calculator
β
5 + 4
β
9
What is the Weather in Multan?
Expected flow:
User Message
β
get_weather
β
Multan
β
Weather Result
If the weather tool fails, the agent retries up to 3 times.
This project demonstrates the following Agentic AI concepts:
- Tool Calling
- Tool Registry
- Decorators
- Routing
- Argument Extraction
- JSON Function Calls
- Tool Execution
- Retry Logic
- Reason β Act β Observe
- Conversation History
- Execution Logging
This project is a small simulation of how an AI agent can interact with external tools.
Instead of directly answering every request, the agent:
- Understands the request using simple rules.
- Selects an appropriate tool.
- Extracts the required arguments.
- Creates a JSON-style function call.
- Executes the selected tool.
- Retries if the tool fails.
- Observes the result.
- Generates a final answer.
- Stores the conversation history.
- Saves the execution log.