Skip to main content
PlanningAgent extends BaseAgent and specialises it for task decomposition. Given a high-level objective it returns a structured list of step strings suitable for handing off to an ExecutionAgent.

Import

from agents import PlanningAgent

Constructor

PlanningAgent(
    llm: BaseChatModel,
    system_prompt: str = "<expert planning prompt>",  # see default below
    agent_name: str = "PlanningAgent"
)
llm
BaseChatModel
required
The LangChain chat model used for plan generation.
system_prompt
str
Override the default system instruction. The built-in prompt instructs the model to act as an expert planning agent and return only a valid JSON object with a "plan" key:
You are an expert planning agent. Your task is to analyze the user's
request and create a structured, step-by-step plan to achieve the goal.
Return ONLY a valid JSON object containing a 'plan' key, which is a list
of strings describing each step.
Example: {"plan": ["Step 1: Do X", "Step 2: Do Y"]}
agent_name
str
default:"PlanningAgent"
Display name used in log messages. Inherited from BaseAgent.

Methods

generate_plan

generate_plan(task: str) -> List[str]
Calls invoke(task), strips any markdown fences from the response, parses the JSON payload, and returns the value of the "plan" key as a list of strings.
task
str
required
The high-level objective to decompose into steps.
Returns: List[str] — ordered list of step descriptions. Expected LLM response format:
{
  "plan": [
    "Step 1: Research the topic",
    "Step 2: Outline the structure",
    "Step 3: Write the first draft"
  ]
}
If the model returns malformed JSON (or wraps the response in a ```json fence that cannot be cleaned), the JSON parse failure is logged at WARNING level and the raw response string is returned as a single-element list: [raw_response].

invoke (inherited)

See BaseAgent.invoke().

Usage example

from langchain_ollama import ChatOllama
from agents import PlanningAgent

llm = ChatOllama(model="llama3")
planner = PlanningAgent(llm=llm)

task = "Build a personal portfolio website"
steps = planner.generate_plan(task)

# steps is a List[str]
for i, step in enumerate(steps, 1):
    print(f"{i}. {step}")

# Example output:
# 1. Step 1: Define the website's purpose and target audience
# 2. Step 2: Choose a tech stack (HTML/CSS/JS or a framework)
# 3. Step 3: Design the layout and colour palette
# 4. Step 4: Implement the pages and deploy to a hosting service
Pass steps directly to an ExecutionAgent inside a loop to build a simple plan-and-execute workflow.

Build docs developers (and LLMs) love