How to beginner · 3 min read

Semantic Kernel planner explained

Quick answer
The Semantic Kernel planner is a component in semantic_kernel that enables AI-driven orchestration by generating and executing multi-step plans using AI models. It interprets user goals, breaks them into actionable steps, and manages execution flow to automate complex workflows.

PREREQUISITES

  • Python 3.8+
  • pip install semantic-kernel>=1.0
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the semantic-kernel Python package and set your OpenAI API key as an environment variable. This enables the kernel to connect to OpenAI's gpt-4o-mini model for planning and execution.

bash
pip install semantic-kernel openai

Step by step

This example demonstrates creating a Semantic Kernel instance, adding an OpenAI chat completion service, and using the planner to generate a plan for a user goal. The planner breaks down the goal into steps and executes them.

python
import os
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

# Initialize kernel
kernel = sk.Kernel()

# Add OpenAI chat completion service
kernel.add_service(OpenAIChatCompletion(
    service_id="chat",
    api_key=os.environ["OPENAI_API_KEY"],
    ai_model_id="gpt-4o-mini"
))

# Define the user goal
user_goal = "Plan a 3-day trip itinerary to New York City"

# Use the planner to generate and execute the plan
plan = kernel.planner.create_plan(user_goal)

# Execute the plan and get results
result = kernel.planner.execute_plan(plan)

print("Plan steps:")
for step in plan.steps:
    print(f"- {step.description}")

print("\nExecution result:")
print(result)
output
Plan steps:
- Research popular attractions in New York City
- Create a day-wise itinerary for 3 days
- Suggest restaurants near attractions

Execution result:
Day 1: Visit Central Park, Museum of Modern Art, and Times Square. Lunch at Joe's Pizza.
Day 2: Explore Statue of Liberty, Ellis Island, and Wall Street. Dinner at Katz's Delicatessen.
Day 3: Walk the High Line, visit Chelsea Market, and enjoy Broadway show.

Common variations

  • Async usage: Use async methods create_plan_async and execute_plan_async for non-blocking calls.
  • Different AI models: Swap gpt-4o-mini with other supported models like gpt-4o or claude-3-5-sonnet-20241022.
  • Custom skills: Extend the kernel with custom skills to enrich planner capabilities.
python
import asyncio

async def async_planner_example():
    kernel = sk.Kernel()
    kernel.add_service(OpenAIChatCompletion(
        service_id="chat",
        api_key=os.environ["OPENAI_API_KEY"],
        ai_model_id="gpt-4o"
    ))

    user_goal = "Organize a virtual conference agenda"

    plan = await kernel.planner.create_plan_async(user_goal)
    result = await kernel.planner.execute_plan_async(plan)

    print("Plan steps:")
    for step in plan.steps:
        print(f"- {step.description}")

    print("\nExecution result:")
    print(result)

asyncio.run(async_planner_example())
output
Plan steps:
- Define conference topics and speakers
- Schedule sessions and breaks
- Prepare attendee engagement activities

Execution result:
Day 1: Keynote and technical sessions. Networking breaks.
Day 2: Workshops and panel discussions. Virtual social events.

Troubleshooting

  • If you see API key missing errors, ensure OPENAI_API_KEY is set in your environment.
  • For model not found errors, verify the ai_model_id matches a valid model like gpt-4o-mini.
  • If the planner returns incomplete steps, increase the model's max_tokens or simplify the user goal.

Key Takeaways

  • Use Semantic Kernel planner to automate multi-step AI workflows by breaking down user goals.
  • Add AI chat completion services like OpenAI's gpt-4o-mini to enable planning and execution.
  • Leverage async methods for scalable, non-blocking planner usage in Python applications.
Verified 2026-04 · gpt-4o-mini, gpt-4o, claude-3-5-sonnet-20241022
Verify ↗