If you work in tech, your mornings likely begin by opening Slack, checking Jira, and scanning your emails, often ending with a messy list of tasks to tackle. Before you even start coding or analyzing data, you spend energy just deciding what to do first. Today, we’ll build a task planning AI agent to help with that.
In this article, we’ll build a lightweight AI agent in Python that takes your messy list of tasks and turns it into a clear, prioritized schedule.
Task Planning AI Agent: Getting Started
We’re building an agent that works as both a parser and a decision-maker. You’ll give it a raw string of text, your unorganized thoughts, and it will sort them using a set prioritization matrix.
Before you start coding, make sure you have a local LLM set up. Install Ollama, which lets you run open-source models on your own machine with an easy-to-use interface.
Once installed, pull the model that we will use for this project:
ollama pull llama3.2
This command downloads a lightweight but powerful model that can handle tasks like scheduling and prioritizing.
Also, install the Ollama library in your Python environment using this command:
pip install ollama
Now, let’s start building the task planning AI agent.
Step 1: Defining the Agent’s Persona and Rules
The heart of our agent is the system prompt. Here, you’ll take the role of a senior engineer giving clear instructions to a junior teammate.
It’s important to be very clear about how tasks should be prioritized and exactly how the output should look:
import ollama
import json
# 1. Define the Agent's Persona and Rules
SYSTEM_PROMPT = """
You are an expert productivity AI agent. Your job is to take an unstructured list of tasks
from a professional and convert them into a structured, prioritized daily schedule.
Rules for prioritization:
1. P0 (Critical): Production bugs, urgent client requests, or hard deadlines today.
2. P1 (High): Core project work, unblocking other team members.
3. P2 (Medium): Routine tasks, emails, documentation.
4. P3 (Low): Nice-to-haves, learning, long-term planning.
Output Constraints:
You MUST respond ONLY with valid JSON. Do not include any conversational text, markdown formatting blocks (like ```json), or explanations.
The JSON schema must match this exact format:
{
"schedule": [
{
"task": "Task name",
"priority": "P0/P1/P2/P3",
"estimated_minutes": 30,
"reasoning": "Brief explanation of why it was prioritized this way"
}
]
}
"""Pay attention to the Output Constraints section. LLMs often try to be helpful and chatty, sometimes starting responses with “Here is the JSON you requested!” This can break your code’s JSON parser.
To build reliable AI features, make sure to forbid conversational text and provide an exact schema.
Step 2: The Unstructured Input
Next, let’s look at what real human input looks like.
Here’s an example of how you might jot down your thoughts in a text editor during your morning coffee:
# 2. The User's Unstructured Input
messy_brain_dump = """
I need to review Sarah's pull request for the login page, it's blocking her.
Also the database is throwing 500 errors on the production reporting dashboard,
need to look at that immediately.
Should probably reply to the marketing team about the new analytics tracking.
I want to spend some time reading the new generative AI paper that came out.
Oh, and I have a 1:1 with my manager at 2 PM, I need to prep some notes for that.
"""Step 3: Calling the Local LLM
Now, let’s set up the function that will handle communication with our local LLM:
# 3. Call the Local LLM
def generate_schedule(user_input):
print("Agent is thinking... (running locally via Ollama)\n")
response = ollama.chat(
model='llama3.2',
messages=[
{'role': 'system', 'content': SYSTEM_PROMPT},
{'role': 'user', 'content': user_input}
],
options={'temperature': 0.1}
)In this step, we set the temperature to 0.1. Temperature controls how random the model’s output is. A high temperature, like 0.8, makes the model more creative, good for poetry. A low temperature makes the model more predictable and precise.
When you need the LLM to follow a strict JSON format and prioritize tasks using a matrix, it’s best to use the lowest temperature possible.
Step 4: Extracting and Parsing the JSON
Finally, we’ll take the raw string from the model and try to parse it into a Python dictionary:
# 4. Extract and Parse the JSON
raw_output = response['message']['content']
try:
# Parse the JSON string into a Python dictionary
plan = json.loads(raw_output)
# Display the formatted schedule
print("🗓️ YOUR OPTIMIZED SCHEDULE:\n" + "-"*30)
for item in plan.get('schedule', []):
print(f"[{item['priority']}] {item['task']} ({item['estimated_minutes']} min)")
print(f" ↳ {item['reasoning']}\n")
except json.JSONDecodeError:
print("Error: The agent did not return valid JSON. Here is the raw output:")
print(raw_output)
# Run the function
if __name__ == "__main__":
generate_schedule(messy_brain_dump)Agent is thinking... (running locally via Ollama)
🗓️ YOUR OPTIMIZED SCHEDULE:
------------------------------
[P0] Review Sarah's pull request for login page (60 min)
↳ Blocking issue
[P0] Investigate database 500 errors on production reporting dashboard (90 min)
↳ Immediate fix needed
[P2] Reply to marketing team about new analytics tracking (30 min)
↳ Routine task
[P3] Read new generative AI paper (60 min)
↳ Learning opportunity
[P2] Prepare notes for 1:1 with manager at 2 PM (30 min)
↳ Preparation
In real-world AI engineering, always assume the LLM might make mistakes or break formatting rules. By wrapping your JSON parser in a try/except block, your app can handle errors smoothly if the model adds something unexpected, instead of crashing.
Closing Thoughts
That’s how you can build a task planning AI agent with Python. This approach turns messy human input into clear, actionable output using an LLM. You’ll find this pattern in many modern AI systems, from task automation tools to enterprise copilots.
If you want to work in AI or ML, this way of thinking is more important than just learning another model architecture. It’s about building systems that truly help people, not just models that generate text.
I hope you enjoyed the article. Follow me on Instagram for more AI and machine learning tips. You can also check out my book, Hands-On GenAI, LLMs & AI Agents, to get career-ready in AI.





