LLM Planner replacement spec¶
Where this page belongs
This is a design spec in the Design section. It covers the mechanisms and the reasons behind the design decisions, not procedures or commands.
This is a specification for replacing the planner in Run the regional safety assistant with an LLM-based planner (LLM: large language model).
The implementation steps are covered in Let an AI plan the steps (LLM Planner); this page is a design document that defines what to fix and where to replace.
What this page covers¶
- the design boundary when replacing the planner with an LLM planner
- the reason for keeping the existing evaluator / actuator / API as they are
- the reason for deciding allow lists, validators, and fallback first
Common pitfalls¶
- treating "adding an LLM" as "turning the whole system into AI" breaks the design
- at the specification stage, responsibility separation comes before implementation steps
- the reason for constraining output to structured JSON instead of free text is easy to overlook
Goal¶
The planner in the current Phase 3 sample (rule_based_planner.py) is rule-based: it picks the location, event, and actions by keyword matching.
For learning purposes it is easy to follow, but it is weak against variation in natural-language phrasing.
The next step uses an LLM to:
- interpret user requests more flexibly
- map them into a fixed JSON structure rather than generating free text
- reuse the existing
evaluatorandactuatorfor evaluation and control
Assumption of This Spec¶
Only the planner layer is replaced. The following parts are reused as they are:
assistant/app/evaluator.pyassistant/app/actuator.pyassistant/app/models.pyPOST /assistant/planPOST /assistant/execute
The design is that the LLM handles planning but not the execution of control.
Design Principles¶
1. Limit the responsibility of the LLM¶
The LLM is responsible only for:
- reading the intent of the request
- estimating the target area
- selecting watch events
- proposing threshold candidates
- selecting candidate actions
The following are not left to the LLM:
- evaluating the actual count of events
- the final trigger decision
- operating the actual devices
- storing audit logs
2. Output must be structured JSON, not free text¶
The LLM output is constrained to a structure close to the existing ExecutionPlan, because leaving it as natural text makes the downstream pipeline unstable.
Minimal output example:
{
"intent": "monitor_public_safety",
"target_area": "park-north",
"time_window_minutes": 30,
"watch_events": ["possible_littering", "suspicious_activity"],
"thresholds": {
"possible_littering": 3,
"suspicious_activity": 1
},
"actions": [
{
"action_type": "light_on",
"target": "park-north-light-1",
"parameters": {"brightness": 80}
},
{
"action_type": "send_notification",
"target": "park-north-manager",
"parameters": {"channel": "mobile_push"}
}
]
}
Input and Output Contract¶
Input¶
request_text- list of allowed events
- list of allowed actions
- list of allowed target areas
Output¶
- JSON that can be converted into
ExecutionPlan
On error¶
If the LLM output is invalid, it is not used directly; fall to one of the following:
- fall back to the rule-based planner
- return
plan_errorand prompt for re-entry
For the initial implementation, falling back to the rule-based planner is the safer option.
Allow Lists¶
The LLM is not allowed to invent event names or action names. It must select only from the allow lists.
Allowed events¶
possible_litteringsuspicious_activityperson_detected
Allowed actions¶
light_onsend_notificationshow_warning
Allowed areas¶
park-northpark-southstation-frontunknown-area(when the location cannot be determined)
If the LLM returns a value outside these sets, the planner rejects it.
Recommended Architecture¶
flowchart LR
A["request_text"] --> B["LLM Planner Adapter"]
B --> C["JSON Validator"]
C --> D["ExecutionPlan"]
D --> E["Evaluator"]
E --> F["Actuator"]
C --> G["Fallback Rule-based Planner"]
Rather than embedding the LLM directly into the core, separate it into adapter + validator + fallback.
Implemented Modules¶
The following files are in assistant/app/.
assistant/app/planner_interface.pyassistant/app/rule_based_planner.pyassistant/app/llm_planner.pyassistant/app/llm_provider.pyassistant/app/llm_prompt.pyassistant/app/plan_validator.pyassistant/app/planner_factory.py
Roles:
planner_interface.py- common planner interface (
plan()and diagnostics) rule_based_planner.py- rule-based planner that builds a plan from keywords (also used as the fallback)
llm_planner.py- builds a plan with the LLM and falls back on failure
llm_provider.py- calls the LLM (stub and OpenAI-compatible API)
llm_prompt.py- builds the system prompt and user prompt
plan_validator.py- checks against the allow lists (pydantic shape validation is done by
ExecutionPlan.model_validateinllm_planner.py) planner_factory.py- switches between
rule_basedandllm
Prompt Requirements¶
At minimum, the LLM is given the following:
- you are the planner for a regional-safety assistant
- output JSON only
- do not output anything outside the allowed events and actions
- return
unknown-areawhen unclear - return thresholds as integers
Bad examples:
- returning a long explanation
- inventing new event names
- guessing and returning execution results
Good example:
- returning minimal JSON that matches the existing schema
Validation Requirements¶
Structural validation¶
- the output must be valid JSON
- required keys must exist
- types must match
Constraint validation¶
watch_eventsmust contain only allowed eventsactions[].action_typemust contain only allowed actionstarget_areamust be an allowed area orunknown-area
Practical validation¶
- the same request does not vary widely
- both Japanese and English requests work at a minimum level
- ambiguous requests do not cause unsafe actions to be added on their own
Evaluation Points¶
In the exercise, check the following:
- explain the difference between the rule-based planner and the LLM planner
- explain both the flexibility and the danger of the LLM
- explain why a validator and fallback are necessary
- explain the design reason for "not leaving everything to the LLM"
Non-Goals¶
This stage does not yet cover:
- multi-turn dialogue
- long-term memory
- autonomous replanning
- agents that freely operate arbitrary devices
The scope for now is only mapping natural language into the existing plan schema.
Recommended Minimal Implementation Steps¶
- fix the
Plannerinterface - add
LLMPlanner - add a JSON validator
- add a fallback
- switch with the
ASSISTANT_PLANNER_MODE=rule_based|llmenvironment variable - confirm JSON validation and fallback with pytest
Expected Learning Outcome¶
- learners can treat an LLM as a constrained component rather than an all-purpose generator
- learners can understand what should stay deterministic when AI is introduced
- learners learn the mindset of extending Phase 3 steadily, without rushing it into a future agent stage (the step after Phase 3)