Skip to main content

Example: Code Generation with .orb

This example shows an agent constructing an .orb schema from a natural language description. The agent uses Masar to plan the construction order, the LLM to generate each piece, and verification to catch errors before they compound.

The Task

User request: "Build a task management app with boards, columns, and draggable cards."

Step 1: Match to a Golden Behavior

from masar import MasarClient

client = MasarClient()

# Find the closest built-in behavior
embedding = client.embed(schema={"description": "task management with boards and draggable cards"})
matches = client.find_similar_behaviors(vector=embedding.vector, limit=3)

for m in matches:
print(f"{m.behavior_name}: {m.similarity:.2f}")
# std-kanban: 0.93
# std-task-manager: 0.78
# std-project-board: 0.74

Step 2: Plan Instructions

plan = client.plan_instructions(
current={"name": "TaskApp", "orbitals": []},
goal="std-kanban",
domain="task-management"
)

print(f"Plan: {plan.total_instructions} instructions, {plan.levels} levels")

Output:

Plan: 18 instructions, 8 levels

Step 3: Iterative Construction

The LLM executes each instruction. Verification runs at each dependency level boundary:

schema = {"name": "TaskApp", "orbitals": []}
current_level = 0

for instr in plan.instructions:
# When we finish a level, verify before moving on
if instr.level > current_level and current_level > 0:
check = client.verify(schema=schema)
print(f"Level {current_level} complete. Valid: {check.valid} ({check.probability:.0%})")

if not check.valid:
errors = client.error_check(schema=schema)
repairs = client.rank_edits(schema=schema, errors=errors.top_errors)
repair_prompt = f"Apply these repairs: {repairs.suggestions[0].actions}"
schema = your_llm.execute(repair_prompt, schema=schema)

current_level = instr.level

# LLM executes the instruction
prompt = f"""
Current schema: {schema}
Instruction: {instr.action}
Parameters: {instr.params}
Apply this instruction and return the updated schema.
"""
schema = your_llm.execute(prompt)

Step 4: Final Validation

final = client.verify(schema=schema)
errors = client.error_check(schema=schema)

if final.valid and not errors.top_errors:
print(f"Schema valid ({final.probability:.0%}). Ready to compile.")
else:
print(f"Issues found: {errors.top_errors}")
# One more repair cycle
repairs = client.rank_edits(schema=schema, errors=errors.top_errors)
schema = your_llm.apply_repairs(schema, repairs.suggestions[0])

Why This Works

Without Masar, the LLM would generate the entire schema in one shot. That works for simple cases but fails on complex ones: missed transitions, orphan states, broken wiring between traits. By decomposing the task into 18 ordered steps with verification checkpoints, errors get caught and fixed at the level where they occur, not 15 steps later when everything is tangled.

Next Steps