Skip to main content

Core Concepts

LSJI is built around two complementary paradigms:

  1. RL Agent Framework — Classical reinforcement learning (Q-Learning, environments)
  2. LLM Agent Framework — Production-grade LLM-based agents with HITL, durability, budget controls

RL Agent Framework (Legacy)​

Architecture Overview​

┌─────────────────────────────────────────────────────────────┐
│ Agent │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
│ │ QLearning │ │ Storage │ │ Env │ │
│ │ (Engine) │◄─┤ (Backend) │ │ (Environment) │ │
│ └─────────────┘ └─────────────┘ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘

1. Environment (Env)​

The Env interface defines the problem domain:

abstract class Env {
getState(): string; // Current state representation
step(action: number): Promise<StepResult>; // Execute action
actionSize(): number; // Number of possible actions
reset(): Promise<string>; // Reset to initial state
}

StepResult contains:

  • state — New state after action
  • reward — Reward received (-1, 0, 1)
  • done — Whether episode ended
  • info — Additional diagnostic info

2. Q-Learning Engine (QLearning)​

Tabular Q-Learning with Temporal Difference (TD) updates:

class QLearning {
constructor({ alpha, gamma, epsilon, storage });

// Epsilon-greedy action selection
async act(state: string, actionSize: number): Promise<number>;

// Full TD update: Q(s,a) ← Q(s,a) + α[r + γ·max Q(s',a') - Q(s,a)]
async learn(state, action, reward, nextState, nextActionSize);

// Simplified update (terminal states): Q(s,a) ← Q(s,a) + α[r - Q(s,a)]
async learnSimple(state, action, reward);

// Get all Q-values for inspection
async getFullQTable(): Promise<QTableRecord[]>;
}

Hyperparameters:

  • alpha (0.1) — Learning rate
  • gamma (0.9) — Discount factor
  • epsilon (0.1) — Exploration rate

3. Storage Backend (Storage)​

Pluggable persistence layer:

BackendPackageUse Case
SqliteStoragenode:sqlite (built-in)Recommended — Zero dependencies
BetterSqliteStoragebetter-sqlite3High-performance synchronous access
MemoryStorageBuilt-inTesting, CI, ephemeral workloads

4. Agent (Agent)​

High-level orchestration:

class Agent {
constructor({ qlearning, storage, env });

async train({ episodes, actionSelector, batchSize });
async play(options?): Promise<PlayResult>;
async status(): Promise<StatusInfo>;
async start(): Promise<{status, message}>;
async stop(): Promise<{status, message}>;
setEnvironment(env): void;
}

LLM Agent Framework (v0.3+)​

Architecture Overview​

┌─────────────────────────────────────────────────────────────────────┐
│ LLMAgent │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ LLM │ │ Tools │ │ Memory │ │ Prompt │ │
│ │ Providers │ │ Registry │ │ Systems │ │ Manager │ │
│ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Execution Engine (Durability) │ │
│ │ ┌────────────┐ ┌────────────┐ ┌────────────┐ ┌──────────┐ │ │
│ │ │ Checkpoint │ │ Budget │ │ HITL │ │Idempotent│ │ │
│ │ │ Store │ │ Control │ │ (Approvals)│ │ Store │ │ │
│ │ └────────────┘ └────────────┘ └────────────┘ └──────────┘ │ │
│ └─────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘

1. LLM Providers​

Unified interface for multiple providers:

ProviderModelsConfig
Geminigemini-1.5-flash, gemini-1.5-pro, gemini-2.0-flashGEMINI_API_KEY
OpenAIgpt-4o, gpt-4o-mini, gpt-4-turboOPENAI_API_KEY
Anthropicclaude-3-5-sonnet, claude-3-opus, claude-3-haikuANTHROPIC_API_KEY
Local (Ollama)Any local modelOLLAMA_HOST

2. Tool Registry​

Manages available tools with built-in HITL & idempotency:

interface ToolDefinition {
name: string;
description: string;
parameters: JSONSchema; // Parameters schema for LLM
requiresApproval: boolean; // Pause for human approval
idempotent: boolean; // Deduplicate on retry
execute(params, context): Promise<any>;
}

Built-in Tools:

ToolCategoryApprovalDescription
web_searchresearchNoSearch the web
file_readfilesystemNoRead a file
file_writefilesystemYesWrite a file
code_execcodeYesExecute code (sandboxed)
api_callnetworkYesHTTP API call
send_emailcommunicationYesSend email
db_querydataYesExecute SQL query

3. Memory Systems​

Three complementary memory types:

MemoryPurposePersistence
ConversationChat history for contextSession + optional DB
EpisodicTask execution traces (steps, tools, results)DB, queryable
SemanticLong-term knowledge, factsVector DB (future)

4. Production Systems​

Budget Control​

  • TokenCounter — Accurate token counting per provider
  • CostTracker — Per-run, daily, monthly limits with USD costs
  • CircuitBreaker — Auto-stops on repeated failures

HITL (Human-in-the-Loop)​

  • ApprovalGate — Request/approve/reject/expire workflows
  • ApprovalStore — Persistent storage with TTL
  • Notifier — Console, webhook, email channels

Durability​

  • ExecutionEngine — Checkpoint every N steps
  • Recovery — Resume from latest checkpoint after crash
  • Workflow ID — Track multi-step executions

Idempotency​

  • IdempotencyStore — Key-based deduplication
  • TTL — Auto-expire after 24h (configurable)
  • Auto-generate — executeAuto() for convenience

Data Flow​

RL Training Loop​

for each episode:
1. Get current state from Env
2. Select action via QLearning.act() (ε-greedy)
3. Execute action in Env → StepResult
4. Update Q-table via QLearning.learnSimple()
5. Persist battle record to Storage
6. Batch DB writes for performance

LLM Agent Execution (ReAct Pattern)​

1. Build system prompt with tool definitions
2. LLM generates response (thought + optional tool calls)
3. For each tool call:
a. Check HITL approval if required
b. Check idempotency key
c. Execute tool
d. Record in episodic memory
e. Return result to LLM
4. Loop until final answer (no tool calls)
5. Track tokens/cost in budget controller
6. Checkpoint state periodically

Reward System (RPS Example)​

OutcomeJudge FormulaReward
Win(ai - user + 3) % 3 = 2+1
Lose(ai - user + 3) % 3 = 1-1
Draw(ai - user + 3) % 3 = 00

Training Patterns (RL)​

PatternIDDescription
Random0Uniform random actions
Always Rock1Always play action 0
Counter2Play counter to previous action
Sequential3Cycle through 0,1,2,0,1,2...

Custom patterns via actionSelector function.


Key Differences: RL vs LLM Agents​

AspectRL AgentLLM Agent
LearningQ-table updates via TDIn-context (no weight updates)
StateDiscrete string statesConversation + memory
ActionsFixed discrete setDynamic tool calls
Explorationε-greedyTemperature + prompt
PersistenceQ-table in SQLiteCheckpoints + episodic memory
Human ControlStart/stop trainingHITL per sensitive action
Cost ControlN/ABudget + circuit breaker