Skip to content

Agent Patterns

SecondBrain ships a canonical agent-pattern library in brain/patterns/.

Purpose

  • expose reusable reasoning and execution patterns
  • keep them usable offline during development
  • share one provider/tool/result model across patterns

Active Pattern Set

The current public surface is centered on seven core patterns plus composition and async helpers.

Core Patterns

Pattern Use It For
ReActAgent tool use and iterative observation loops
PlanAndExecuteAgent explicit planning before step execution
ReflexionAgent self-critique and bounded retries
MultiAgentOrchestrator parallel subtasks with synthesis
RAGAgent retrieval-augmented question answering
StreamingReActAgent event-by-event streaming UIs
HITLAgent approval-gated tool use

Additional Public Helpers

  • Agent as a higher-level entrypoint
  • PatternChain and PatternRouter for composition
  • async variants such as AsyncReActAgent and run_concurrent()
  • testing helpers such as AgentHarness and MockProvider

Quick Start

from brain.patterns import ReActAgent, RAGAgent

agent = ReActAgent()
result = agent.run("What is retrieval-augmented generation?")
print(result.answer)

rag = RAGAgent(retriever=my_retriever)
result = rag.run("What changed in our auth subsystem?")
print(result.answer)

Common Supporting Utilities

The patterns package also includes:

  • typed tool helpers
  • prompt registry primitives
  • tracing helpers
  • conversation-memory helpers
  • deterministic testing support

These live in the same package so pattern consumers do not need to reinvent the supporting runtime every time.

Choosing A Pattern

  • choose ReActAgent when you need tool use
  • choose PlanAndExecuteAgent when the task benefits from explicit decomposition
  • choose ReflexionAgent when answer quality matters more than raw latency
  • choose RAGAgent when retrieval quality is the main concern
  • choose HITLAgent when the tool surface should stop for review
  • choose MultiAgentOrchestrator only when subtasks are genuinely parallelizable

Implementation Pointers

  • brain/patterns/__init__.py
  • brain/patterns/react.py
  • brain/patterns/plan_execute.py
  • brain/patterns/reflexion.py
  • brain/patterns/rag.py
  • brain/patterns/multi_agent.py
  • brain/patterns/testing.py