agentleFS
kienbui1995kienbui1995/magic-powers/skills/agentic-ai-patterns/SKILL.md

agentic-ai-patterns

Skill0 starsChanged 8 months agoMITView on GitHub

Use when designing AI agents - tool use, multi-agent orchestration, state management, planning loops, error recovery, and agent evaluation

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Graceful timeout: If no response within timeout → escalate or abort safely, never proceed on assumption.

    line 178
  2. Distinctive:

    Use smaller model for tool selection, larger for final synthesis

    line 210

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Agentic AI Patterns

Overview

An agent is an LLM in a loop: observe → think → act → observe. The hard part isn't the LLM — it's the loop control, error recovery, and knowing when to stop.

When to Use

  • Building AI agents that take actions (not just chat)
  • Designing tool-use patterns
  • Orchestrating multiple agents
  • Debugging agent loops that get stuck or go off-track

Agent Architecture

User Goal → Planner → [Tool Call → Observe → Decide]* → Final Answer

Core Components

ComponentPurpose
PlannerBreak goal into steps
ExecutorRun tools, API calls
ObserverParse tool results
DeciderContinue, retry, or stop
MemoryTrack state across steps

Patterns

1. ReAct (Reasoning + Acting)

Thought: I need to find the user's order status
Action: query_database(user_id=123, table="orders")
Observation: [{"id": 456, "status": "shipped"}]
Thought: Found it. The order is shipped.
Answer: Your order #456 has been shipped.

2. Plan-and-Execute

Plan: 1) Search docs  2) Extract relevant info  3) Summarize
Execute step 1: search("refund policy") → [doc1, doc2]
Execute step 2: extract(doc1) → "30-day refund window..."
Execute step 3: summarize → "You have 30 days to request a refund."

3. Multi-Agent (specialist delegation)

Router Agent → classify intent
  ├── Research Agent → search + summarize
  ├── Code Agent → write + test code
  └── Data Agent → query + analyze data

Graph-Based Agents (LangGraph)

Beyond simple loops, production agents use directed graphs for complex branching logic:

from langgraph.graph import StateGraph, END

def should_continue(state):
    if state["tool_calls"]: return "tools"
    if state["needs_human"]: return "human_review"
    return END

graph = StateGraph(AgentState)
graph.add_node("llm", call_llm)
graph.add_node("tools", execute_tools)
graph.add_node("human_review", request_approval)
graph.add_conditional_edges("llm", should_continue)
graph.add_edge("tools", "llm")  # loop back after tool execution

Key patterns:

  • Parallel tool execution — fan-out to multiple tools simultaneously, merge results
  • Conditional branching — route based on output content, confidence, or tool results
  • Subgraph composition — nest agent graphs for modularity
  • Persistence — LangGraph's checkpointing enables pause/resume across sessions

Tool Design Rules

  1. Clear names — search_documents not tool_1
  2. Typed parameters — JSON schema for every tool
  3. Bounded output — truncate large results, paginate
  4. Error messages — return actionable errors, not stack traces
  5. Idempotent — safe to retry on failure

Error Recovery

FailureRecovery
Tool returns errorRetry once, then report to user
Agent loops >10 stepsForce stop, summarize progress
Off-topic driftCheck goal alignment every 3 steps
Hallucinated tool callValidate tool name exists before calling
TimeoutSet max execution time, graceful exit

State Management

state = {
    "goal": "Find cheapest flight to Tokyo",
    "steps_completed": ["searched flights", "compared prices"],
    "current_step": "booking confirmation",
    "attempts": 2,
    "max_attempts": 5,
    "context": {...}
}
  • Persist state for long-running agents
  • Log every step for debugging
  • Set hard limits: max steps, max time, max cost

Memory Architecture

Agents need different memory types for different purposes:

TypeStorageLifetimeUse case
In-contextToken windowCurrent sessionActive task state, recent tool results
SessionDB (Redis/Postgres)One conversationUser preferences, conversation history
Long-termVector DBPersistentUser facts, past decisions, learned patterns
EpisodicDB + embeddingsPersistentPast task completions, examples

Context window management:

# Summarize old messages to prevent overflow
def compress_history(messages, max_tokens=4000):
    if count_tokens(messages) < max_tokens:
        return messages
    # Keep system + last 5 messages, summarize the rest
    summary = llm.summarize(messages[1:-5])
    return [messages[0], HumanMessage(f"[Summary: {summary}]")] + messages[-5:]

When to use external memory:

  • Conversation > 20 turns → summarize and store
  • User mentions facts that apply beyond this session → upsert to long-term store
  • Agent needs to "remember" past tasks → episodic store with semantic search

Human-in-the-Loop

Design confidence-based escalation rather than binary human/autonomous:

def route_by_confidence(result, confidence_threshold=0.85):
    if result.confidence >= confidence_threshold:
        return "auto_proceed"
    elif result.confidence >= 0.6:
        return "notify_and_proceed"  # log but continue
    else:
        return "require_approval"    # block and wait

# Approval checkpoint in LangGraph
def human_approval_node(state):
    # Pause execution, notify human, wait for response
    send_notification(state["pending_action"])
    approval = wait_for_human_input(timeout=3600)  # 1 hour timeout
    return {"approved": approval, "human_feedback": approval.comment}

When to require human approval:

  • Irreversible actions (delete, send, purchase, deploy)
  • Low confidence + high stakes
  • Novel situation not seen in training
  • User explicitly requested oversight
  • Regulatory requirement (financial, medical)

Graceful timeout: If no response within timeout → escalate or abort safely, never proceed on assumption.

Anti-Patterns

PatternFix
No max iterationsAlways set a loop limit
Agent calls itself recursivelyDetect cycles, break loop
Too many tools (>15)Group into categories, use router
No loggingLog every thought/action/observation
Trusting agent output blindlyValidate before executing side effects

Cost-Aware Agent Design

Agent costs compound: each step adds tokens. Design for efficiency:

Token budgets:

class CostAwareAgent:
    def __init__(self, max_tokens_per_task=50000):
        self.token_budget = max_tokens_per_task
        self.tokens_used = 0
    
    def should_continue(self, step_estimate):
        if self.tokens_used + step_estimate > self.token_budget * 0.9:
            return "summarize_and_stop"  # graceful degradation
        return "continue"

Tool selection strategy:

  • Cheap tools first (search before synthesize)
  • Cache tool results for duplicate calls in same session
  • Use smaller model for tool selection, larger for final synthesis
  • Parallel tools when independent (fan-out saves latency and doesn't add cost)

Per-task cost tracking:

# Log cost per agent task for accountability
log_task_cost(task_id, input_tokens, output_tokens, tool_calls, total_usd)

Integration

  • magic-powers:prompt-engineering — design agent system prompts
  • magic-powers:llm-evaluation — evaluate agent task completion
  • magic-powers:ai-safety-guardrails — guard agent actions
  • magic-powers:llm-observability — monitor agent runs

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.