Skip to content

Advanced LLM Features

Status: Stable (supplements operations-reference.md) Scope: Deep patterns for controlling, structuring, optimizing, and validating @llm driven workflows beyond the core syntax.


Overview

Fractalic treats every @llm invocation as a deterministic transformation over an evolving document tree. Advanced usage focuses on:

  • Precision context shaping (selective block referencing, staged compression)
  • Tool / agentic loop governance (bounded reasoning, explicit decision frames)
  • Structured + schema‑validated outputs
  • Multi-agent orchestration (planner / executor / reviewer / router)
  • Performance, token, and cost optimization
  • Regression safety (golden outputs, diff contracts)
  • Guardrails (content policies, redaction, JSON validation)

Use this document as a pattern catalog; operational field definitions remain canonical in the Operations & Syntax references.


Prompt Engineering Patterns

PatternGoalTechnique
Context Window SculptingMinimize noiseReference only needed leaf blocks; avoid wide /* early
Section TaggingEnable selective refinementEmbed stable IDs {id=analysis-raw} then later target them
Role DecompositionIncrease reasoning quality"You are acting as: (1) Planner (2) Analyst (3) Synthesizer" with explicit deliverables
Progressive CompressionPreserve signalRaw → bullet summary → prioritized list → canonical spec
Delta PromptingFocus revision"ONLY adjust tone; keep structure & bullet counts identical"
Explicit TerminationAvoid runaway tool loops"Call a tool ONLY if factual gap remains. Otherwise answer FINAL." / add stop criteria list
Reflection InjectionQuality gatingAfter draft: second @llm with rubric to critique and patch

Inline Example (planning then refining selected block):

markdown
# Investigation Plan {id=plan-v1}
... initial content ...

@llm
prompt: "Refine ONLY the plan keeping step count fixed. Improve clarity only."
block:
  - plan-v1/*
mode: replace
to: plan-v1  # In-place refinement

Structured Output Strategies

TechniqueNotes
use-header: noneRemoves wrapper heading for pure machine output
Strict JSON EnvelopeInstruct: "Return ONLY valid JSON matching schema" + stop sequence guards
Schema EchoHave model first restate expected keys (short) then produce final JSON (higher adherence)
Dual PhaseGenerate reasoning (hidden / discarded) then final JSON (by instructing: "Output ONLY final JSON after thinking internally")
Failure DetectionPost-parse validator: if invalid, re-run with appended error explanation block

Example (schema guard):

markdown
@llm
prompt: |
  Produce ONLY JSON: {"title": str, "risks": [{"id": str, "severity": "low|med|high"}]}
  No prose.
use-header: none
stop-sequences:
  - "\n# "

Tool / Agentic Loop Advanced Controls

AspectPattern
Decision FramingProvide explicit checklist: "Call tool if ANY of: missing metric, unresolved ID, >1 conflicting claim"
Turn Budgetingtools-turns-max: 2 (typical). Raise only with measured benefit
Selective Tool SetNarrow tools: list per phase (gather vs synthesize)
Result CanonicalizationImmediately summarize verbose tool outputs into compact blocks to avoid snowballing context
Attribution IDsInstruct tool loop to emit headings with stable {id=...} for reuse
Abort Clause"If tool returns no actionable data, respond FINAL directly"

Multi-Agent Composition Patterns

Pattern | Flow | Notes Planner / Executor | planner.md (@return spec) → executor consumes spec | Keeps large raw exploration isolated Executor / Reviewer | executor output → reviewer critiques → patch pass | Ensures quality gating Router | lightweight router agent decides which specialized agent file to invoke via fractalic_run | Reduces average cost Self-Refinement Loop | generation → critique rubric → constrained rewrite | Limit to 1–2 cycles to avoid drift

Router Decision Example:

markdown
@llm
prompt: |
  Decide: if task mentions "deploy" run deploy agent else run default agent. Respond with JUST the agent call arguments.
tools:
  - fractalic_run

Determinism & Reproducibility

GoalTechnique
Minimize nondeterminismStable model versions, explicit temperature (if provider supports), avoid ambiguous instructions
TraceabilityCommit intermediate blocks; use replace instead of endless append growth
Stable ReferencesAdd {id=...} to reusable blocks before externalizing
Change AuditingPair large replacements with a summarized diff block for human review
Idempotent ReplaysAvoid date / transient calls inside reasoning; fetch externally then import snapshot

Token & Cost Optimization

StrategyDetail
Layered SummariesRaw import → compressed → ultra-compact semantic index
Context Bloom ControlReplace large exploratory text once distilled
Media PruningOnly attach images that materially change outcome
Selective WildcardsUse explicit IDs after first pass, not section/* repeatedly
External File Parkingsave-to-file for bulky logs; re-import narrow extracts
Compression Prompt"Rewrite the following preserving all numbers & entities; max 120 tokens"

Safety & Guardrails

ConcernMitigation
Sensitive Data LeakageRedact via preprocessing shell step before @llm
Unbounded Tool ActionsProvide explicit negative instructions ("Never execute destructive shell commands")
Prompt Injection from ImportsSanitize or summarize third-party text before inclusion
Toxic OutputPost-generation validator agent that scans and blocks policy violations
Schema PoisoningValidate JSON; on failure, re-run with appended validator error explanation

Validator Pattern:

markdown
@llm
prompt: "Check the prior block for PII; respond PASS or list redactions needed."
block:
  block_uri: draft-output/*

Evaluation & Regression Testing

MethodImplementation
Golden Output SnapshotsStore canonical JSON / text; diff on regeneration
Metric ExtractionSecondary @llm producing structured rubric scores (clarity, completeness, risk)
Factual ConsistencyCompare entity lists across versions; flag additions / deletions
Test HarnessShell step running unit tests produced by model before acceptance
Drift DetectionHash compressed summaries; change triggers deeper review

Retrieval & External Knowledge

NeedPattern
Static Knowledge@import curated handbook sections, not raw dumps
Dynamic FetchShell / MCP retrieval tool → immediate summarization block
Lightweight RAGPrecompute embedding clusters (external), import only matched cluster summaries
Cache LayerStore previous tool responses; fallback if rate-limited

Model Routing & Fallback

ScenarioApproach
Cheap Draft, Expensive PolishFirst pass small model; second pass high-quality model referencing draft
Structured ExtractionSmaller deterministic model often adequate; escalate only on complexity detection
Automatic FallbackRouter agent interprets error block & retries with alternate model

Routing Skeleton:

markdown
@llm
prompt: |
  If task complexity (steps > 5 or domain specialized) -> choose model "deep" else "fast". Output ONLY: model=`<id>`.
use-header: none

Strict JSON Mode Recipe

  1. Provide minimal schema in natural language
  2. Set use-header: none
  3. Provide explicit invalid examples (optional) to steer away
  4. Add narrow stop sequences that terminate at first blank line or heading
  5. Post-parse; if error → re-run with appended parser message under # JSON Parse Error {id=json-err}

Snippet:

markdown
@llm
prompt: |
  Return ONLY JSON with keys: items (array of {id, label, priority:int 1-5}), summary:str.
  No code fences, no comments.
use-header: none
stop-sequences:
  - "\n# "

Parallelization Patterns

Use separate files / agents for independent subtasks then import results for synthesis. Workflow:

  1. Split tasks (A, B, C) each as @run or direct @llm
  2. Each produces compact summary with stable ID
  3. Synthesis call references only */summary blocks

Benefits: reduces cross-contamination, keeps token usage bounded.


Debugging Tool Loops

SymptomDebug Step
Repeated Tool CallsAdd explicit termination rule; inspect last tool result content
Empty Final AnswerCheck raw tool JSON for missing return_content
Lost ContextConfirm correct block IDs passed (wildcard vs leaf mismatch)
Heading ExplosionEnable header-auto-align or constrain model to avoid re-heading tool outputs
Unexpected Import SizeInstrument with a shell token count before invoking @llm

Tracing Tip: Centralize debug mode that sets tools-turns-max: 1 + adds a diagnostic block summarizing selected blocks & token counts.


Comprehensive Checklist

CategoryQuestions
ContextAre only necessary blocks referenced? Any stale large blocks replaceable?
StructureStable IDs assigned where downstream reuse expected?
ToolsClear call criteria & bounded turns? Redundant tools removed?
OutputDoes it need raw JSON (use-header: none)? Schema validated?
SafetyRedaction / policy validation steps present for sensitive domains?
PerformanceSummaries replacing verbose intermediates? Large outputs externalized?
EvaluationGolden outputs or rubric scoring present for critical workflows?
RecoveryFallback / retry model plan defined?

See Also

  • Operations Reference (§5.3.*) for field semantics
  • Syntax Reference for YAML field grammar
  • (Upcoming) Context Management doc for AST merge & selection internals

Revision: v1.0

Released under the MIT License.