agentic-patterns ยท v5.0.0 ยท 2026-09-15 ยท sha256 5709674b817ba5da

agentic-patterns v5.0.0A

Immutable. This exact content never changes and is served at /api/v1/blob/5709674b817ba5da.

---
name: agentic-patterns
description: Use when AI agent design principles. Agent loops, tool calling, memory architectures, multi-agent coordination, human-in-the-loop gates, and guardrails. Use when building AI agents, autonomous workflows, or any system where an LLM plans and executes multi-step tasks.
version: 5.0.0
last-updated: 2026-09-13
skills:
  - agent-organizer
  - fabel-protocol
  - thinking-protocol
tools: Read, Grep, Glob, Bash, Edit, Write
scripts-binding:
  - .agent/scripts/swarm_dispatcher.js
  - .agent/scripts/verify_all.js
  - .agent/scripts/lint_runner.js
---

# Agentic Patterns

## Mandatory Pre-Flight Context Inspection

Before generating, refactoring, or reviewing code in the `agentic-patterns` domain, inspect these 5 critical parameters:

1. **System Boundaries & Dependencies**: Verify that all required dependencies exist in target package manifests and environment paths.
2. **Runtime Context & Platform Invariants**: Confirm target platform constraints (Node.js, Browser, Mobile OS, Edge runtime) before applying APIs.
3. **Execution Guardrails**: Identify potential side-effects, state mutations, and unhandled asynchronous exceptions.
4. **Validation & Type Contracts**: Validate input data schemas and strict type constraints across all module interfaces.
5. **Observability & Proof of Execution**: Ensure execution produces tangible verification signals (terminal output, tests, metrics).

## Activation Boundaries

- **Activate when:** Use when AI agent design principles. Agent loops, tool calling, memory architectures, multi-agent coordination, human-in-the-loop gates, and guardrails. Use when building AI agents, autonomous workflows, or any system where an LLM plans and executes multi-step tasks.
- **DO NOT activate when:** The task falls outside the `agentic-patterns` domain or is managed by a different dedicated specialist.

## ๐Ÿ” Multi-Pass Execution Protocol

Execute all non-trivial tasks through this 7-pass cognitive loop:

| Pass       | Phase            | Core Action                                                                                  |
| :--------- | :--------------- | :------------------------------------------------------------------------------------------- |
| **Pass 1** | **Understand**   | Deconstruct the user's explicit objective, implicit requirements, and platform constraints.  |
| **Pass 2** | **Plan**         | Decompose the task into smallest logical steps; map dependencies and required tool calls.    |
| **Pass 3** | **Execute**      | Implement the solution with production-grade craft, zero placeholders, and strict typing.    |
| **Pass 4** | **Verify**       | Run linters, unit tests, or compiler checks to validate structural correctness.              |
| **Pass 5** | **Attack**       | Perform an adversarial review searching for edge-case failures, race conditions, and traps.  |
| **Pass 6** | **Improve**      | Eliminate discovered friction, optimize performance, and harden error boundaries.            |
| **Pass 7** | **Quality Gate** | Enforce Verification-Before-Completion (VBC) with concrete terminal proof before finalizing. |

---

## ๐Ÿ› ๏ธ Technical Architecture & Reference Recipes

---

---

## The Agent Loop

Every AI agent follows this fundamental pattern:

```
PERCEIVE โ†’ PLAN โ†’ ACT โ†’ OBSERVE โ†’ (repeat or terminate)

1. PERCEIVE   โ€” What is the current state? What does the agent know?
2. PLAN       โ€” What action will move toward the goal?
3. ACT        โ€” Execute the tool, call the API, write the file
4. OBSERVE    โ€” What changed? Did the action succeed?
5. EVALUATE   โ€” Goal reached? Continue loop or return?
```

### When to Terminate

```ts
// The three termination conditions โ€” always define all three
type AgentResult = {
  reason: 'goal_reached' | 'max_steps_exceeded' | 'human_escalation';
  steps: number;
  result: string;
};

const MAX_STEPS = 10; // Hard cap โ€” never let agents loop indefinitely
```

---

## Tool Calling Design

Tools are the agent's interface to the real world. Design them defensively:

```ts
// Tool definition โ€” what the LLM sees and how to call it
const tools = [
  {
    type: 'function',
    function: {
      name: 'search_database',
      description:
        'Search the product database. Use this before creating a new record to avoid duplicates.',
      parameters: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'Search terms โ€” be specific',
          },
          limit: {
            type: 'number',
            description: 'Max results to return. Default: 5, max: 20',
          },
        },
        required: ['query'],
      },
    },
  },
];

// Tool executor โ€” validate before running
async function executeTool(name: string, args: unknown): Promise<string> {
  // Validate args before executing โ€” never trust LLM output directly
  const parsed = ToolArgsSchema.safeParse(args);
  if (!parsed.success) {
    return `Error: Invalid arguments โ€” ${parsed.error.message}`;
  }

  // Scope check โ€” is this tool allowed for this agent's role?
  if (!agentPermissions.includes(name)) {
    return `Error: Tool '${name}' is not permitted for this agent`;
  }

  try {
    return await tools[name](parsed.data);
  } catch (err) {
    return `Error: Tool execution failed โ€” ${(err as Error).message}`;
  }
}
```

---

## Memory Architecture

Agents need different types of memory for different purposes:

```
IN-CONTEXT MEMORY (cheapest, shortest-lived):
  โ†’ Current conversation + recent tool outputs
  โ†’ Limited by context window (~100k tokens)
  โ†’ Good for: current task context

EXTERNAL SEMANTIC MEMORY (vector search):
  โ†’ Long-term knowledge, past conversations
  โ†’ Unlimited, but retrieval is approximate
  โ†’ Good for: "What did we discuss about this topic before?"

EPISODIC MEMORY (structured log):
  โ†’ Exact record of past actions and outcomes
  โ†’ Good for: learning from past mistakes, auditability

PROCEDURAL MEMORY (system prompt + tools):
  โ†’ How the agent knows to behave and what it can do
  โ†’ Good for: skills, personas, behavior rules
```

```ts
// External memory: retrieve relevant past context before each turn
async function buildContext(userId: string, currentQuery: string) {
  const queryEmbedding = await embed(currentQuery);

  // Retrieve semantically relevant past interactions
  const pastMemories = await vectorDB.search({
    query: queryEmbedding,
    filter: { userId },
    limit: 5,
  });

  return [
    { role: 'system', content: systemPrompt },
    // Inject relevant past context โ€” NOT entire history
    {
      role: 'system',
      content: `Relevant past context:\n${pastMemories.map(m => m.content).join('\n')}`,
    },
    { role: 'user', content: currentQuery },
  ];
}
```

---

## Multi-Agent Coordination Patterns

When a task requires multiple specialists:

### Supervisor Pattern

```
Supervisor agent โ”€โ†’ breaks task into subtasks
    โ”‚
    โ”œโ”€โ†’ Research agent   (reads, gathers information)
    โ”œโ”€โ†’ Writer agent     (drafts based on research)
    โ””โ”€โ†’ Reviewer agent   (critiques the draft)
         โ”‚
         โ””โ”€โ†’ Supervisor collects results, makes final decision
```

### Peer Review Pattern (Anti-Hallucination for Agents)

```ts
// Two independent agents answer the same question โ€” supervisor resolves disagreement
const [answerA, answerB] = await Promise.all([
  agentA.complete(question),
  agentB.complete(question),
]);

if (answerA.answer === answerB.answer) {
  return answerA; // Agreement โ€” high confidence
}

// Disagreement โ€” escalate to human or third tiebreaker
return await supervisor.resolve(question, answerA, answerB);
```

---

## Human-in-the-Loop Gates

The most important agentic pattern. Agents should request human approval before:

- Deleting data
- Sending external communications (emails, webhooks)
- Spending real money (API calls with cost, purchases)
- Making irreversible changes
- Acting on low-confidence decisions

```ts
async function agentLoop(task: string) {
  for (let step = 0; step < MAX_STEPS; step++) {
    const planned = await llm.plan(task, history);

    // โœ… Human gate before irreversible actions
    if (planned.action.isIrreversible) {
      const approved = await requestHumanApproval({
        action: planned.action,
        reason: planned.reasoning,
        confidence: planned.confidence,
      });
      if (!approved) return { reason: 'human_rejected', step };
    }

    // โœ… Confidence gate โ€” don't act when uncertain
    if (planned.confidence < 0.7) {
      return {
        reason: 'human_escalation',
        message: `Low confidence (${planned.confidence}) on: ${planned.action.description}`,
      };
    }

    const result = await executeTool(planned.action.tool, planned.action.args);
    history.push({ action: planned.action, result });

    if (planned.goalReached) break;
  }
}
```

---

## Guardrails

Every production agent needs:

```ts
const guardrails = {
  // Input guardrails โ€” reject bad prompts before they reach the agent
  input: [
    { check: 'no_prompt_injection', action: 'reject' },
    { check: 'within_scope', action: 'reject' }, // Off-topic requests
    { check: 'pii_detection', action: 'redact' }, // Redact before processing
  ],

  // Output guardrails โ€” validate before returning
  output: [
    { check: 'no_hallucinated_citations', action: 'flag' },
    { check: 'schema_valid', action: 'retry_once' },
    { check: 'no_pii_leaked', action: 'reject' },
  ],

  // Resource guardrails โ€” prevent runaway cost/loops
  resource: [
    { check: 'max_tokens_per_session', limit: 100_000 },
    { check: 'max_tool_calls_per_session', limit: 50 },
    { check: 'max_cost_per_session_usd', limit: 1.0 },
  ],
};
```

---

## Output Format

When this skill completes a task, structure your output as:

```
โ”โ”โ” Agentic Patterns Output โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
Task:        [what was performed]
Result:      [outcome summary โ€” one line]
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
Checks:      โœ… [N passed] ยท โš ๏ธ  [N warnings] ยท โŒ [N blocked]
VBC status:  PENDING โ†’ VERIFIED
Evidence:    [link to terminal output, test result, or file diff]
```

---

## ๐Ÿšจ Edge-Case & Failure Mode Matrix

| Scenario                               | Risk                                                 | Mitigation Strategy                                                             |
| :------------------------------------- | :--------------------------------------------------- | :------------------------------------------------------------------------------ |
| **Empty or Null Inputs**               | Unhandled exception or unexpected rendering collapse | Enforce fallback guards, optional chaining, and explicit empty state handlers   |
| **Network Timeout / Latency**          | Hanging operations or duplicate side-effects         | Implement bounded abort controllers, exponential backoff, and idempotency keys  |
| **Concurrency / Race Conditions**      | Stale state overwrite or inconsistent data mutations | Use atomic transactions, mutex locking, or cancel-on-resubmit controls          |
| **Invalid Schema / Malformed Payload** | Downstream runtime errors or security injection      | Validate boundary payloads with Zod/Pydantic schemas prior to execution         |
| **Resource / Memory Saturation**       | OOM errors, frame drops, or memory leaks             | Clean up listeners, cancel active timers, and enforce pagination/virtualization |

---

## ๐Ÿค– LLM-Specific Traps Table

| Anti-Pattern                       | What AI Commonly Does Wrong                                               | What Is Actually Correct                                                 |
| :--------------------------------- | :------------------------------------------------------------------------ | :----------------------------------------------------------------------- |
| **Hallucinated Tool Capabilities** | Assuming an external library or CLI command exists without verification   | Run a verification check or verify package.json before referencing tools |
| **Premature Completion Claim**     | Declaring a task finished because code was generated without verification | Execute tests, linters, or terminal commands to provide concrete proof   |
| **Context Bloat Dumping**          | Pasting entire multi-thousand-line files into prompt context              | Extract targeted excerpts, symbols, and signatures to preserve tokens    |

---

## ๐Ÿ›๏ธ Tribunal Verification & Guardrails

**Active Reviewers:** `orchestrator` ยท `agent-organizer` ยท `logic-reviewer`
**Slash Command:** `/review` or `/tribunal-full`

### โœ… Pre-Flight Self-Audit Checklist

```
โœ… Did I deconstruct the root objective before proposing architecture?
โœ… Did I identify dependencies, bottlenecks, and parallelizable sub-tasks?
โœ… Did I avoid over-engineering and select the simplest effective pattern?
โœ… Did I verify assumptions with concrete file reads instead of speculation?
โœ… Did I establish measurable verification criteria before completion?
```

### ๐Ÿ›‘ Verification-Before-Completion (VBC) Protocol

**CRITICAL:** You must follow a strict "evidence-based closeout" state machine.

- โŒ **Forbidden:** Declaring a task complete because the output "looks correct."
- โœ… **Required:** You are explicitly forbidden from finalizing any task without providing **concrete evidence** (terminal output, passing test suites, compiler success, or equivalent operational proof) that your output works as intended.