Skip to content

Self-improving AI Employees for everyone

feat: integrate RLM agent into MachinaOS (phases 1-6)

trohitgc1b92fdf3cc7
diff --git a/CLAUDE.md b/CLAUDE.md
index 4e2f93cc..4883f046 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -781,7 +781,7 @@ Search API nodes that work BOTH as standalone workflow nodes AND as AI Agent too
- Brave Search and Perplexity in **Search** category
- Serper in **Scrapers** category (Google SERP scraping)
-### Specialized AI Agents (13 nodes)
+### Specialized AI Agents (14 nodes)
Specialized agents are AI Agents pre-configured for specific domains. They inherit full AI Agent functionality (provider, model, prompt, system message, thinking/reasoning) while being tailored for specific capabilities. All specialized agents route to `handle_chat_agent` in the backend and support the same input handles. Node colors use centralized dracula theme constants imported from `client/src/styles/theme.ts`.
**Input Handles:**
@@ -805,16 +805,18 @@ Specialized agents are AI Agents pre-configured for specific domains. They inher
- **autonomous_agent**: Autonomous Agent - AI agent for autonomous operations using Code Mode patterns. Uses agentic loops, progressive discovery, error recovery, and multi-tool orchestration for 81-98% token savings. Connect autonomous skills via Master Skill.
- **orchestrator_agent**: Orchestrator Agent - Team lead agent for coordinating multiple agents. Connect specialized agents via `input-teammates` handle; they become `delegate_to_*` tools the AI can invoke.
- **ai_employee**: AI Employee - Team lead agent similar to orchestrator_agent. Connect specialized agents via `input-teammates` handle for intelligent task delegation.
+- **rlm_agent**: RLM Agent - Recursive Language Model agent using REPL-based code execution with recursive LM calls. Replaces LangGraph tool-calling with RLM's `exec()` REPL loop (`llm_query()`, `rlm_query()`, `FINAL()`). Routes to dedicated `handle_rlm_agent` handler and `RLMService` (not `handle_chat_agent`). Connect AI chat model nodes as small LMs for depth>=1 calls. See `docs-internal/rlm_service.md`.
**Backend Routing:**
-Specialized agents are detected by `SPECIALIZED_AGENT_TYPES` and routed to `handle_chat_agent`:
+Specialized agents are detected by `SPECIALIZED_AGENT_TYPES` and routed to `handle_chat_agent` (except `rlm_agent` which routes to `handle_rlm_agent`):
```python
-# In node_executor.py - all specialized agents route to handle_chat_agent
+# In node_executor.py - most specialized agents route to handle_chat_agent
SPECIALIZED_AGENT_TYPES = {
'android_agent', 'coding_agent', 'web_agent', 'task_agent', 'social_agent',
'travel_agent', 'tool_agent', 'productivity_agent', 'payments_agent', 'consumer_agent',
- 'autonomous_agent', 'orchestrator_agent', 'ai_employee'
+ 'autonomous_agent', 'orchestrator_agent', 'ai_employee',
}
+# rlm_agent routes to handle_rlm_agent -> RLMService (dedicated handler + service)
```
**Team Lead Types (Agent Teams Pattern):**
@@ -2199,6 +2201,7 @@ The system supports specialized agent variants that inherit from the base AI Age
| Autonomous | `autonomous_agent` | target | purple |
| Orchestrator | `orchestrator_agent` | conductor | cyan |
| AI Employee | `ai_employee` | briefcase | purple |
+| RLM Agent | `rlm_agent` | brain | orange |
All specialized agents share the same handle configuration:
- **Left**: `input-main` (Input, 30%), `input-memory` (Memory, 55%), `input-task` (Task, 85%)
diff --git a/client/src/components/AIAgentNode.tsx b/client/src/components/AIAgentNode.tsx
index 52df6e25..d51e21e8 100644
--- a/client/src/components/AIAgentNode.tsx
+++ b/client/src/components/AIAgentNode.tsx
@@ -344,6 +344,23 @@ const AGENT_CONFIGS: Record<string, AgentConfig> = {
width: 300,
height: 200,
},
+ rlm_agent: {
+ icon: <span style={{ fontSize: '28px' }}>🧠</span>,
+ title: 'RLM Agent',
+ subtitle: 'Recursive Reasoning',
+ themeColorKey: 'orange',
+ bottomHandles: [
+ { id: 'input-skill', label: 'Skill', position: '25%' },
+ { id: 'input-tools', label: 'Tool', position: '75%' },
+ ],
+ leftHandles: [
+ { id: 'input-memory', label: 'Memory', position: '65%' },
+ { id: 'input-task', label: 'Task', position: '85%' },
+ ],
+ topOutputHandle: { id: 'output-top', label: 'Output' },
+ width: 300,
+ height: 200,
+ },
};
const AIAgentNode: React.FC<NodeProps<NodeData>> = ({ id, type, data, isConnectable, selected }) => {
diff --git a/client/src/components/parameterPanel/InputSection.tsx b/client/src/components/parameterPanel/InputSection.tsx
index 35c07d86..d6065af0 100644
--- a/client/src/components/parameterPanel/InputSection.tsx
+++ b/client/src/components/parameterPanel/InputSection.tsx
@@ -89,7 +89,7 @@ const InputSection: React.FC<InputSectionProps> = ({ nodeId, visible = true }) =
'aiAgent', 'chatAgent', 'android_agent', 'coding_agent',
'web_agent', 'task_agent', 'social_agent', 'travel_agent',
'tool_agent', 'productivity_agent', 'payments_agent', 'consumer_agent',
- 'autonomous_agent', 'orchestrator_agent', 'ai_employee'
+ 'autonomous_agent', 'orchestrator_agent', 'ai_employee', 'rlm_agent'
];
// Check if current node is an agent that shows skills in Middle Section
const isAgentWithSkills = AGENT_WITH_SKILLS_TYPES.includes(currentNodeType || '');
@@ -729,7 +729,7 @@ const InputSection: React.FC<InputSectionProps> = ({ nodeId, visible = true }) =
const isMemory = nodeType === 'simpleMemory';
const nodeTypeLower = nodeType.toLowerCase();
// AI agent types that use the AI output schema
- const aiAgentTypes = ['aiAgent', 'chatAgent', 'android_agent', 'coding_agent', 'web_agent', 'task_agent', 'social_agent', 'travel_agent', 'tool_agent', 'productivity_agent', 'payments_agent', 'consumer_agent', 'autonomous_agent', 'orchestrator_agent', 'ai_employee'];
+ const aiAgentTypes = ['aiAgent', 'chatAgent', 'android_agent', 'coding_agent', 'web_agent', 'task_agent', 'social_agent', 'travel_agent', 'tool_agent', 'productivity_agent', 'payments_agent', 'consumer_agent', 'autonomous_agent', 'orchestrator_agent', 'ai_employee', 'rlm_agent'];
const isAI = !isMemory && (nodeTypeLower.includes('chatmodel') || aiAgentTypes.includes(nodeType));
const isFile = nodeType.includes('file');
const isWhatsAppDb = nodeType === 'whatsappDb';
diff --git a/client/src/components/parameterPanel/MiddleSection.tsx b/client/src/components/parameterPanel/MiddleSection.tsx
index dbe9af82..a21e7443 100644
--- a/client/src/components/parameterPanel/MiddleSection.tsx
+++ b/client/src/components/parameterPanel/MiddleSection.tsx
@@ -32,7 +32,8 @@ const AGENT_WITH_SKILLS_TYPES = [
'consumer_agent',
'autonomous_agent',
'orchestrator_agent',
- 'ai_employee'
+ 'ai_employee',
+ 'rlm_agent'
];
interface ConnectedSkill {
diff --git a/client/src/nodeDefinitions/specializedAgentNodes.ts b/client/src/nodeDefinitions/specializedAgentNodes.ts
index 5a4c02cf..fd9a51eb 100644
--- a/client/src/nodeDefinitions/specializedAgentNodes.ts
+++ b/client/src/nodeDefinitions/specializedAgentNodes.ts
@@ -399,6 +399,62 @@ export const specializedAgentNodes: Record<string, INodeTypeDescription> = {
description: 'Maximum concurrent task executions'
}
]
+ },
+
+ // RLM Agent - Recursive Language Model with REPL-based reasoning
+ rlm_agent: {
+ displayName: 'RLM Agent',
+ name: 'rlm_agent',
+ icon: '🧠',
+ group: ['agent', 'ai'],
+ version: 1,
+ subtitle: 'Recursive Reasoning',
+ description: 'Recursive Language Model agent. Uses REPL code execution with recursive LM calls for complex reasoning tasks.',
+ defaults: { name: 'RLM Agent', color: dracula.orange },
+ inputs: AI_AGENT_INPUTS,
+ outputs: AI_AGENT_OUTPUTS,
+ properties: [
+ ...AI_AGENT_PROPERTIES,
+ {
+ displayName: 'Max Iterations',
+ name: 'maxIterations',
+ type: 'number',
+ default: 30,
+ typeOptions: { minValue: 1, maxValue: 100 },
+ description: 'Maximum REPL loop iterations before stopping'
+ },
+ {
+ displayName: 'Max Depth',
+ name: 'maxDepth',
+ type: 'number',
+ default: 1,
+ typeOptions: { minValue: 0, maxValue: 5 },
+ description: 'Maximum recursion depth for rlm_query() calls'
+ },
+ {
+ displayName: 'Max Budget ($)',
+ name: 'maxBudget',
+ type: 'number',
+ default: 0,
+ typeOptions: { minValue: 0 },
+ description: 'Maximum USD spend (0 = unlimited)'
+ },
+ {
+ displayName: 'Max Timeout (s)',
+ name: 'maxTimeout',
+ type: 'number',
+ default: 0,
+ typeOptions: { minValue: 0 },
+ description: 'Maximum execution time in seconds (0 = unlimited)'
+ },
+ {
+ displayName: 'Verbose',
+ name: 'verbose',
+ type: 'boolean',
+ default: false,
+ description: 'Enable detailed REPL output logging'
+ }
+ ]
}
};
@@ -407,4 +463,4 @@ export const specializedAgentNodes: Record<string, INodeTypeDescription> = {
// ============================================================================
// List of specialized agent node types for identification
-export const SPECIALIZED_AGENT_TYPES = ['android_agent', 'coding_agent', 'web_agent', 'task_agent', 'social_agent', 'travel_agent', 'tool_agent', 'productivity_agent', 'payments_agent', 'consumer_agent', 'autonomous_agent', 'orchestrator_agent', 'ai_employee'];
+export const SPECIALIZED_AGENT_TYPES = ['android_agent', 'coding_agent', 'web_agent', 'task_agent', 'social_agent', 'travel_agent', 'tool_agent', 'productivity_agent', 'payments_agent', 'consumer_agent', 'autonomous_agent', 'orchestrator_agent', 'ai_employee', 'rlm_agent'];
diff --git a/docs-internal/node_creation.md b/docs-internal/node_creation.md
index f8127840..6935c9ea 100644
--- a/docs-internal/node_creation.md
+++ b/docs-internal/node_creation.md
@@ -26,7 +26,7 @@ This guide explains how to create new nodes for MachinaOs, covering frontend def
## Node Types Overview
-MachinaOs supports 111 nodes across 19 categories:
+MachinaOs supports 112 nodes across 19 categories:
| Category | Visual Component | Example Nodes | Count |
|----------|-----------------|---------------|-------|
@@ -34,7 +34,7 @@ MachinaOs supports 111 nodes across 19 categories:
| **Scheduler** | SquareNode | timer, cronScheduler | 2 |
| **AI Chat Models** | SquareNode | openaiChatModel, anthropicChatModel, geminiChatModel, openrouterChatModel, groqChatModel, cerebrasChatModel | 6 |
| **AI Agents** | AIAgentNode | aiAgent, chatAgent | 2 |
-| **Specialized Agents** | AIAgentNode | android_agent, coding_agent, web_agent, task_agent, social_agent, travel_agent, tool_agent, productivity_agent, payments_agent, consumer_agent, autonomous_agent, orchestrator_agent | 12 |
+| **Specialized Agents** | AIAgentNode | android_agent, coding_agent, web_agent, task_agent, social_agent, travel_agent, tool_agent, productivity_agent, payments_agent, consumer_agent, autonomous_agent, orchestrator_agent, rlm_agent | 13 |
| **AI Memory** | SquareNode | simpleMemory | 1 |
| **AI Skills** | SquareNode | masterSkill, customSkill + 9 individual skills | 11 |
| **AI Tools** | SquareNode | calculatorTool, currentTimeTool, duckduckgoSearch, androidTool | 4 |
diff --git a/docs-internal/rlm_service.md b/docs-internal/rlm_service.md
new file mode 100644
index 00000000..222478aa
--- /dev/null
+++ b/docs-internal/rlm_service.md
@@ -0,0 +1,321 @@
+# RLM Service -- Recursive Language Model Agent Integration
+
+## Overview
+
+The RLM (Recursive Language Models) service integrates the `rlms` library into MachinaOS as a specialized agent node (`rlm_agent`). Unlike other specialized agents that route to `handle_chat_agent` and use LangGraph, the RLM agent uses its own REPL-based code execution loop where the LM writes Python code blocks that are `exec()`-ed in a sandboxed environment.
+
+**Library**: [rlms](https://pypi.org/project/rlms/) (pip install rlms)
+**Paper**: https://arxiv.org/abs/2512.24601
+**Docs**: https://alexzhang13.github.io/rlm/
+
+### How RLM Differs from LangGraph Agents
+
+| Aspect | LangGraph Agents (aiAgent, chatAgent, etc.) | RLM Agent (rlm_agent) |
+|--------|----------------------------------------------|----------------------|
+| Execution model | LLM -> tool call -> LLM -> tool call | LLM -> `\`\`\`repl` code block -> exec() -> stdout -> LLM |
+| Tool interface | LangChain StructuredTool with Pydantic schemas | Python functions injected into REPL namespace |
+| State management | LangGraph StateGraph with message accumulation | Python variables in REPL namespace + `context` variable |
+| Completion signal | LLM stops making tool calls | LLM calls `FINAL(answer)` or `FINAL_VAR(variable_name)` |
+| Recursion | Agent delegation (fire-and-forget) | `rlm_query()` spawns child RLM with own REPL |
+| Strengths | Structured tool calling, provider-agnostic | Complex reasoning, code-driven decomposition, recursive sub-problems |
+
+---
+
+## Architecture
+
+```
+MachinaOS Node Execution System
+================================
+
+[chatTrigger] --input-main--+
+[simpleMemory] --input-memory--+
+[masterSkill] --input-skill--+
+ v
+ +-----------+
+ | rlm_agent |
+ +-----+-----+
+ |
+ input-tools handle
+ / | \
+ [openaiChatModel] [pythonExecutor] [braveSearch]
+ (small LM, (tool node, (tool node,
+ depth>=1) bridged to bridged to
+ custom_tool) custom_tool)
+
+
+Execution Flow
+================================
+
+NodeExecutor._dispatch()
+ -> handle_rlm_agent() [server/services/handlers/rlm.py]
+ |
+ +-- _collect_agent_connections() [REUSE from handlers/ai.py]
+ +-- _format_task_context() [REUSE from handlers/ai.py]
+ +-- tool stripping on completion [REUSE pattern]
+ +-- auto-prompt fallback [REUSE pattern]
+ |
+ v
+ ai_service.rlm_service.execute() [server/services/rlm/service.py]
+ |
+ +-- _build_skill_system_prompt() [REUSE from ai.py]
+ +-- is_model_valid_for_provider() [REUSE from ai.py]
+ +-- get_default_model_async() [REUSE from ai.py]
+ +-- self.auth.get_api_key() [REUSE from AIService]
+ |
+ +-- BackendAdapter.adapt() [server/services/rlm/adapters.py]
+ +-- ChatModelExtractor.extract() [server/services/rlm/adapters.py]
+ +-- ToolBridgeAdapter.bridge() [server/services/rlm/adapters.py]
+ |
+ v
+ asyncio.to_thread(RLM.completion()) [rlm library, sync -> async bridge]
+ |
+ v
+ Result formatting + memory save + broadcast
+```
+
+---
+
+## File Structure
+
+```
+server/services/rlm/
+ __init__.py # Exports RLMService
+ constants.py # Provider maps, base URLs, default parameters
+ adapters.py # BackendAdapter, ChatModelExtractor, ToolBridgeAdapter
+ service.py # RLMService class with execute() method
+
+server/services/handlers/
+ rlm.py # Thin handler (~70 lines, mirrors handle_chat_agent)
+```
+
+---
+
+## LM Architecture
+
+### Main LLM (depth=0)
+The `rlm_agent` node's own provider/model configuration drives the outer REPL loop. This is the LM that writes `\`\`\`repl` code blocks and receives exec() output.
+
+Configured via the standard AI agent parameters:
+- `provider`: openai, anthropic, gemini, groq, openrouter, cerebras
+- `model`: The model name (e.g., gpt-4o, claude-sonnet-4-20250514)
+- `api_key`: From MachinaOS credential store
+
+### Small LMs (depth>=1)
+Connected `AI_CHAT_MODEL_TYPES` nodes (openaiChatModel, anthropicChatModel, etc.) provide backends for `llm_query()` and `rlm_query()` calls made from within the REPL code.
+
+These are extracted by `ChatModelExtractor` and passed to RLM as `other_backends` / `other_backend_kwargs`.
+
+**Example workflow**:
+```
+rlm_agent (gpt-4o) + openaiChatModel (gpt-4o-mini)
+ | |
+ depth=0 depth>=1
+ (writes REPL code) (used by llm_query() inside REPL)
+```
+
+---
+
+## Adapters
+
+### BackendAdapter
+
+Maps MachinaOS provider/model/api_key to RLM backend constructor arguments.
+
+```python
+BackendAdapter.adapt("openai", "gpt-4o", "sk-...")
+# -> ("openai", {"model_name": "gpt-4o", "api_key": "sk-..."})
+
+BackendAdapter.adapt("groq", "llama-3.3-70b-versatile", "gsk-...")
+# -> ("openai", {"model_name": "llama-3.3-70b-versatile", "api_key": "gsk-...", "base_url": "https://api.groq.com/openai/v1"})
+```
+
+**Provider mapping** (constants.py):
+
+| MachinaOS Provider | RLM Backend | Base URL Override |
+|--------------------|-------------|-------------------|
+| openai | openai | -- |
+| anthropic | anthropic | -- |
+| gemini | gemini | -- |
+| groq | openai | https://api.groq.com/openai/v1 |
+| openrouter | openrouter | https://openrouter.ai/api/v1 |
+| cerebras | openai | https://api.cerebras.ai/v1 |
+
+### ChatModelExtractor
+
+Scans `tool_data` (nodes connected to `input-tools`) for `AI_CHAT_MODEL_TYPES` nodes. Extracts their provider/model/api_key and converts via `BackendAdapter` into RLM's `other_backends` format.
+
+Currently RLM supports one `other_backend` (for depth>=1 calls). If multiple chat model nodes are connected, only the first is used.
+
+### ToolBridgeAdapter
+
+Bridges non-agent, non-chat-model tool nodes into RLM `custom_tools` dict format.
+
+**How it works**:
+1. Filters out `AI_AGENT_TYPES` (handled via delegation) and `AI_CHAT_MODEL_TYPES` (handled by ChatModelExtractor)
+2. For each remaining tool node, creates a sync callable wrapper
+3. The wrapper uses `asyncio.run_coroutine_threadsafe()` to call MachinaOS's `execute_tool()` dispatcher from RLM's synchronous REPL thread
+4. Returns dict in RLM custom_tools format: `{"tool_name": {"tool": callable, "description": str}}`
+
+**Why sync wrappers**: RLM's LocalREPL uses `exec()` which runs synchronously. MachinaOS tool handlers are async. The bridge uses `asyncio.run_coroutine_threadsafe()` with a 60-second timeout to cross the sync/async boundary.
+
+**Supported tool nodes** (auto-bridged, no per-tool code needed):
+
+| Tool Node | Bridged Name | REPL Usage |
+|-----------|-------------|------------|
+| pythonExecutor | python_executor | `python_executor(code="output = 2+2")` |
+| httpRequest | http_request | `http_request(url="...", method="GET")` |
+| duckduckgoSearch | duckduckgo_search | `duckduckgo_search(query="...")` |
+| braveSearch | brave_search | `brave_search(query="...")` |
+| serperSearch | serper_search | `serper_search(query="...")` |
+| perplexitySearch | perplexity_search | `perplexity_search(query="...")` |
+| calculatorTool | calculator_tool | `calculator_tool(operation="add", a=1, b=2)` |
+| currentTimeTool | current_time_tool | `current_time_tool(timezone="UTC")` |
+| crawleeScraper | crawlee_scraper | `crawlee_scraper(url="...")` |
+| gmail | gmail | `gmail(operation="send", ...)` |
+| sheets | sheets | `sheets(operation="read", ...)` |
+
+Any tool node connected to `input-tools` is automatically bridged -- no per-tool configuration needed.
+
+---
+
+## RLM-Specific Parameters
+
+These are extracted from the node's `options` object:
+
+| Parameter | Type | Default | Description |
+|-----------|------|---------|-------------|
+| `maxIterations` | int | 30 | Maximum REPL loop iterations before stopping |
+| `maxDepth` | int | 1 | Maximum recursion depth for `rlm_query()` calls |
+| `maxBudget` | float | null | Maximum USD spend across all LM calls (optional) |
+| `maxTimeout` | float | null | Maximum seconds for entire execution (optional) |
+| `maxTokens` | int | null | Maximum tokens across all LM calls (optional) |
+| `verbose` | bool | false | Enable detailed REPL output logging |
+
+---
+
+## Handler Pattern
+
+The handler (`handle_rlm_agent`) follows `handle_chat_agent` exactly:
+
+```python
+# 1. Collect connections (same as handle_chat_agent)
+memory_data, skill_data, tool_data, input_data, task_data = await _collect_agent_connections(
+ node_id, context, database, log_prefix="[RLM Agent]"
+)
+
+# 2. Task context injection (same pattern)
+if task_data:
+ task_context = _format_task_context(task_data)
+ parameters = {**parameters, 'prompt': f"{task_context}\n\n{parameters.get('prompt', '')}"}
+
+ # 3. Tool stripping on task completion (same pattern)
+ if task_status in ('completed', 'error') and tool_data:
+ tool_data = []
+
+# 4. Auto-prompt fallback (same pattern)
+if not parameters.get('prompt') and input_data:
+ prompt = input_data.get('message') or input_data.get('text') or ...
+
+# 5. Delegate to RLM service (differs from handle_chat_agent)
+return await ai_service.rlm_service.execute(node_id, parameters, ...)
+```
+
+---
+
+## Registration Points
+
+| File | Change |
+|------|--------|
+| `server/constants.py` | `'rlm_agent'` in `AI_AGENT_TYPES` frozenset |
+| `server/services/node_executor.py` | `'rlm_agent': partial(handle_rlm_agent, ...)` in `_build_handler_registry()` |
+| `server/services/handlers/__init__.py` | `from .rlm import handle_rlm_agent` |
+| `server/services/ai.py` | `self.rlm_service = RLMService(auth=self.auth)` in `AIService.__init__()` |
+| `server/requirements.txt` | `rlms>=0.1.2` |
+| `server/pyproject.toml` | `"rlms>=0.1.2"` in `[project] dependencies` |
+
+---
+
+## Reused MachinaOS Functions
+
+| Function | Source | Purpose in RLM |
+|----------|--------|----------------|
+| `_collect_agent_connections()` | `handlers/ai.py:17` | Discover memory, skills, tools, input, task connections |
+| `_format_task_context()` | `handlers/ai.py:352` | Format delegated task results as prompt context |
+| `_build_skill_system_prompt()` | `ai.py:833` | Build system prompt from connected skill nodes |
+| `is_model_valid_for_provider()` | `ai.py:362` | Validate model name for provider |
+| `get_default_model_async()` | `ai.py:377` | Look up default model from DB or config |
+| `self.auth.get_api_key()` | AIService | Retrieve API key from credential store |
+| `save_message()` | `memory_store.py` | Persist conversation to memory |
+| `execute_tool()` | `handlers/tools.py:34` | Dispatch tool execution (used by ToolBridgeAdapter) |
+| `broadcaster.update_node_status()` | `status_broadcaster.py` | Real-time UI status updates |
+
+---
+
+## RLM Concepts Quick Reference
+
+### REPL Code Blocks
+The LM writes Python code inside `\`\`\`repl` fenced blocks. These are extracted via regex and executed with `exec()` in a sandboxed namespace.
+
+### Available Functions in REPL
+- `llm_query(prompt)` -- Plain LM call (uses depth+1 backend)
+- `rlm_query(prompt)` -- Recursive child RLM with its own REPL
+- `FINAL(answer)` -- Signal completion with answer string
+- `FINAL_VAR(variable_name)` -- Signal completion, answer is the value of a Python variable
+- `SHOW_VARS()` -- Print current namespace variables (debugging)
+- `context` -- Python variable containing the input prompt text
+
+### Execution Loop
+```
+1. Build prompt from iteration history
+2. Call LM -> get response
+3. Extract ```repl code blocks via regex
+4. Execute code in sandboxed REPL via exec()
+5. Check for FINAL/FINAL_VAR in output
+6. If found -> return answer
+7. Feed stdout back as next iteration context
+8. Repeat until FINAL or limits exceeded
+```
+
+### Resource Limits
+- `max_iterations` -- Loop iteration cap (default 30)
+- `max_depth` -- Recursion depth cap for rlm_query() (default 1)
+- `max_budget` -- USD spend cap
+- `max_timeout` -- Wall-clock time cap (seconds)
+- `max_tokens` -- Total token cap across all LM calls
+- `max_errors` -- Consecutive execution error cap
+
+Each child RLM (via `rlm_query`) gets its own LMHandler + LocalREPL on a separate port. Resource limits propagate: children get remaining budget/timeout, not the original totals.
+
+---
+
+## Status Broadcasting Phases
+
+The RLM service broadcasts these phases via WebSocket for UI animations:
+
+| Phase | When | Details |
+|-------|------|---------|
+| `initializing` | Before RLM creation | provider, model |
+| `building_tools` | Before tool bridge | message |
+| `executing` | During RLM.completion() | max_iterations, max_depth, tool_count |
+| `completed` | After successful completion | iterations count, execution time |
+| `error` | On failure | error message |
+
+---
+
+## Future Phases
+
+### Delegation Support (Phase 4)
+Add `'rlm_agent'` to:
+- `DEFAULT_TOOL_NAMES` -> `'delegate_to_rlm_agent'`
+- `DEFAULT_TOOL_DESCRIPTIONS` -> ONE-SHOT delegation description
+- `_get_tool_schema()` -> `DelegateToAgentSchema` condition
+- `execute_tool()` dispatch tuple in `handlers/tools.py`
+
+### Frontend (Phase 5)
+- Node definition in `specializedAgentNodes.ts`
+- `AGENT_CONFIGS` entry in `AIAgentNode.tsx`
+- Parameter panel integration in `MiddleSection.tsx` and `InputSection.tsx`
+
+### RLM Skill (Phase 6)
+- `server/skills/rlm_agent/rlm-reasoning-skill/SKILL.md`
+- REPL usage instructions, available functions, decomposition strategies
diff --git a/docs-internal/specialized_agent_node_creation.md b/docs-internal/specialized_agent_node_creation.md
index eae028db..5d01072a 100644
--- a/docs-internal/specialized_agent_node_creation.md
+++ b/docs-internal/specialized_agent_node_creation.md
@@ -60,6 +60,9 @@ All agent colors use centralized dracula theme constants from `client/src/styles
| `consumer_agent` | Consumer Agent | Consumer interactions agent | `dracula.purple` |
| `autonomous_agent` | Autonomous Agent | Autonomous operations with Code Mode patterns | `dracula.purple` |
| `orchestrator_agent` | Orchestrator Agent | Multi-agent coordination and task delegation | `dracula.cyan` |
+| `rlm_agent` | RLM Agent | Recursive Language Model agent with REPL code execution | `dracula.orange` |
+
+**Note:** `rlm_agent` is unique among specialized agents -- it routes to `handle_rlm_agent` -> `RLMService` (not `handle_chat_agent`). It uses RLM's REPL-based execution instead of LangGraph. See `docs-internal/rlm_service.md` for full details.
## Shared AI Agent Properties
diff --git a/server/constants.py b/server/constants.py
index abd1c097..f5fa132b 100644
--- a/server/constants.py
+++ b/server/constants.py
@@ -35,6 +35,7 @@ AI_AGENT_TYPES: FrozenSet[str] = frozenset([
'autonomous_agent',
'orchestrator_agent',
'ai_employee',
+ 'rlm_agent',
])
AI_MEMORY_TYPES: FrozenSet[str] = frozenset([
diff --git a/server/pyproject.toml b/server/pyproject.toml
index 4c13deea..62c1d6fb 100644
--- a/server/pyproject.toml
+++ b/server/pyproject.toml
@@ -84,6 +84,9 @@ dependencies = [
# Temporal (durable workflow execution)
"temporalio>=1.7.0",
+
+ # RLM (Recursive Language Models) agent
+ "rlms>=0.1.2",
]
[project.optional-dependencies]
diff --git a/server/requirements.txt b/server/requirements.txt
index 117f802b..7837bc60 100644
--- a/server/requirements.txt
+++ b/server/requirements.txt
@@ -102,3 +102,6 @@ pypdf>=4.0.0
# marker-pdf>=1.0.0 # Optional: GPU OCR (requires CUDA)
# unstructured>=0.16.0 # Optional: Multi-format document parsing
+# RLM (Recursive Language Models) agent
+rlms>=0.1.2
+
diff --git a/server/services/ai.py b/server/services/ai.py
index 9b10a4ba..386dc87a 100644
--- a/server/services/ai.py
+++ b/server/services/ai.py
@@ -910,6 +910,9 @@ class AIService:
self.database = database
self.cache = cache
self.settings = settings
+ # RLM service (lazy import to avoid circular deps)
+ from services.rlm import RLMService
+ self.rlm_service = RLMService(auth=self.auth)
def detect_provider(self, model: str) -> str:
"""Detect AI provider from model name."""
@@ -2405,6 +2408,7 @@ class AIService:
'autonomous_agent': 'delegate_to_autonomous_agent',
'orchestrator_agent': 'delegate_to_orchestrator_agent',
'ai_employee': 'delegate_to_ai_employee',
+ 'rlm_agent': 'delegate_to_rlm_agent',
# Android service nodes (direct tool usage)
'batteryMonitor': 'android_battery',
'networkMonitor': 'android_network',
@@ -2471,6 +2475,7 @@ class AIService:
'autonomous_agent': 'ONE-SHOT delegation to Autonomous Agent. Call ONCE per task, returns task_id. Agent works in background using Code Mode patterns - do NOT re-call.',
'orchestrator_agent': 'ONE-SHOT delegation to Orchestrator Agent. Call ONCE per task, returns task_id. Coordinates multiple agents - do NOT re-call.',
'ai_employee': 'ONE-SHOT delegation to AI Employee. Call ONCE per task, returns task_id. Coordinates multiple agents - do NOT re-call.',
+ 'rlm_agent': 'ONE-SHOT delegation to RLM Agent. Call ONCE per task, returns task_id. Uses recursive REPL-based reasoning with code execution - do NOT re-call.',
# Android service nodes (direct tool usage)
'batteryMonitor': 'Monitor Android battery status, level, charging state, temperature, and health.',
'networkMonitor': 'Monitor Android network connectivity, type, and internet availability.',
@@ -3385,7 +3390,7 @@ class AIService:
return CheckDelegatedTasksSchema
# AI Agent delegation schema (fire-and-forget async delegation)
- if node_type in ('aiAgent', 'chatAgent', 'android_agent', 'coding_agent', 'web_agent', 'task_agent', 'social_agent', 'travel_agent', 'tool_agent', 'productivity_agent', 'payments_agent', 'consumer_agent', 'autonomous_agent', 'orchestrator_agent', 'ai_employee'):
+ if node_type in ('aiAgent', 'chatAgent', 'android_agent', 'coding_agent', 'web_agent', 'task_agent', 'social_agent', 'travel_agent', 'tool_agent', 'productivity_agent', 'payments_agent', 'consumer_agent', 'autonomous_agent', 'orchestrator_agent', 'ai_employee', 'rlm_agent'):
agent_label = params.get('label', node_type)
class DelegateToAgentSchema(BaseModel):
diff --git a/server/services/handlers/__init__.py b/server/services/handlers/__init__.py
index fea79f51..b0db7ae5 100644
--- a/server/services/handlers/__init__.py
+++ b/server/services/handlers/__init__.py
@@ -114,6 +114,9 @@ from .search import (
# Crawlee web scraping handler
from .crawlee import handle_crawlee_scraper
+# RLM agent handler
+from .rlm import handle_rlm_agent
+
# Tool execution handlers (for AI Agent tool calling)
from .tools import (
execute_tool,
@@ -181,6 +184,8 @@ __all__ = [
'handle_perplexity_search',
# Crawlee
'handle_crawlee_scraper',
+ # RLM
+ 'handle_rlm_agent',
# Tools
'execute_tool',
'handle_task_manager',
diff --git a/server/services/handlers/rlm.py b/server/services/handlers/rlm.py
new file mode 100644
index 00000000..f5904135
--- /dev/null
+++ b/server/services/handlers/rlm.py
@@ -0,0 +1,78 @@
+"""RLM agent handler -- thin wrapper following handle_chat_agent pattern.
+
+Reference: docs-internal/specialized_agent_node_creation.md (Step 5)
+"""
+
+from typing import Dict, Any, TYPE_CHECKING
+from core.logging import get_logger
+
+if TYPE_CHECKING:
+ from services.ai import AIService
+ from core.database import Database
+
+logger = get_logger(__name__)
+
+
+async def handle_rlm_agent(
+ node_id: str,
+ node_type: str,
+ parameters: Dict[str, Any],
+ context: Dict[str, Any],
+ ai_service: "AIService",
+ database: "Database",
+) -> Dict[str, Any]:
+ """Handle RLM agent node execution.
+
+ Follows handle_chat_agent pattern (handlers/ai.py:469-564):
+ 1. _collect_agent_connections() [REUSE]
+ 2. _format_task_context() [REUSE]
+ 3. Tool stripping on task completion [REUSE]
+ 4. Auto-prompt fallback [REUSE]
+ 5. Delegate to RLMService.execute()
+ """
+ from .ai import _collect_agent_connections, _format_task_context
+ from services.status_broadcaster import get_status_broadcaster
+
+ workflow_id = context.get('workflow_id')
+
+ # 1. Collect connections (handlers/ai.py:17)
+ memory_data, skill_data, tool_data, input_data, task_data = await _collect_agent_connections(
+ node_id, context, database, log_prefix="[RLM Agent]"
+ )
+
+ # 2. Task context injection (handlers/ai.py:352)
+ if task_data:
+ task_context = _format_task_context(task_data)
+ original_prompt = parameters.get('prompt', '')
+ parameters = {**parameters, 'prompt': f"{task_context}\n\n{original_prompt}"}
+ logger.info(f"[RLM Agent] Task context injected for task_id={task_data.get('task_id')}")
+
+ # 3. Tool stripping on task completion (handlers/ai.py:514-519)
+ task_status = task_data.get('status', '')
+ if task_status in ('completed', 'error') and tool_data:
+ tool_data = []
+ logger.info("[RLM Agent] Stripped tools for task completion handling")
+
+ # 4. Auto-prompt fallback (handlers/ai.py:522-530)
+ if not parameters.get('prompt') and input_data:
+ prompt = (
+ input_data.get('message') or
+ input_data.get('text') or
+ input_data.get('content') or
+ str(input_data)
+ )
+ parameters = {**parameters, 'prompt': prompt}
+ logger.info("[RLM Agent] Auto-using input as prompt")
+
+ # 5. Delegate to RLM service
+ broadcaster = get_status_broadcaster()
+ return await ai_service.rlm_service.execute(
+ node_id, parameters,
+ memory_data=memory_data,
+ skill_data=skill_data if skill_data else None,
+ tool_data=tool_data if tool_data else None,
+ broadcaster=broadcaster,
+ workflow_id=workflow_id,
+ context=context,
+ database=database,
+ )
diff --git a/server/services/handlers/tools.py b/server/services/handlers/tools.py
index efe65b42..7c458810 100644
--- a/server/services/handlers/tools.py
+++ b/server/services/handlers/tools.py
@@ -190,7 +190,7 @@ async def execute_tool(tool_name: str, tool_args: Dict[str, Any],
# AI Agent delegation (fire-and-forget async delegation)
# Includes specialized agents: android_agent, coding_agent, web_agent, task_agent, social_agent, autonomous_agent
- if node_type in ('aiAgent', 'chatAgent', 'android_agent', 'coding_agent', 'web_agent', 'task_agent', 'social_agent', 'travel_agent', 'tool_agent', 'productivity_agent', 'payments_agent', 'consumer_agent', 'autonomous_agent', 'orchestrator_agent', 'ai_employee'):
+ if node_type in ('aiAgent', 'chatAgent', 'android_agent', 'coding_agent', 'web_agent', 'task_agent', 'social_agent', 'travel_agent', 'tool_agent', 'productivity_agent', 'payments_agent', 'consumer_agent', 'autonomous_agent', 'orchestrator_agent', 'ai_employee', 'rlm_agent'):
return await _execute_delegated_agent(tool_args, config)
# Generic fallback for unknown node types
diff --git a/server/services/node_executor.py b/server/services/node_executor.py
index b92b41fe..a37fc3bd 100644
--- a/server/services/node_executor.py
+++ b/server/services/node_executor.py
@@ -38,6 +38,7 @@ from services.handlers import (
handle_task_manager,
handle_twitter_send, handle_twitter_search, handle_twitter_user,
handle_brave_search, handle_serper_search, handle_perplexity_search,
+ handle_rlm_agent,
)
from services.handlers.telegram import handle_telegram_send
from services.handlers.apify import handle_apify_actor
@@ -136,6 +137,7 @@ class NodeExecutor:
'autonomous_agent': partial(handle_chat_agent, ai_service=self.ai_service, database=self.database),
'orchestrator_agent': partial(handle_chat_agent, ai_service=self.ai_service, database=self.database),
'ai_employee': partial(handle_chat_agent, ai_service=self.ai_service, database=self.database),
+ 'rlm_agent': partial(handle_rlm_agent, ai_service=self.ai_service, database=self.database),
'simpleMemory': handle_simple_memory,
# Maps
'gmaps_create': partial(handle_create_map, maps_service=self.maps_service),
diff --git a/server/services/rlm/__init__.py b/server/services/rlm/__init__.py
new file mode 100644
index 00000000..559bd758
--- /dev/null
+++ b/server/services/rlm/__init__.py
@@ -0,0 +1,8 @@
+"""RLM (Recursive Language Models) service module.
+
+Provides RLMService for executing RLM agent completions within MachinaOS.
+"""
+
+from .service import RLMService
+
+__all__ = ["RLMService"]
diff --git a/server/services/rlm/adapters.py b/server/services/rlm/adapters.py
new file mode 100644
index 00000000..3becf947
--- /dev/null
+++ b/server/services/rlm/adapters.py
@@ -0,0 +1,151 @@
+"""Adapters bridging MachinaOS systems to RLM interfaces.
+
+- BackendAdapter: MachinaOS provider config -> RLM backend + backend_kwargs
+- ChatModelExtractor: Connected chat model nodes -> RLM other_backends
+- ToolBridgeAdapter: Connected tool nodes -> RLM custom_tools dict
+"""
+
+import asyncio
+import re
+from typing import Dict, Any, List, Tuple, Optional
+
+from core.logging import get_logger
+from .constants import PROVIDER_TO_BACKEND, PROVIDER_BASE_URLS
+
+logger = get_logger(__name__)
+
+
+class BackendAdapter:
+ """Maps MachinaOS provider/model/api_key to RLM backend constructor args."""
+
+ @staticmethod
+ def adapt(provider: str, model: str, api_key: str) -> Tuple[str, Dict[str, Any]]:
+ backend = PROVIDER_TO_BACKEND.get(provider, 'openai')
+ kwargs = {"model_name": model, "api_key": api_key}
+ if provider in PROVIDER_BASE_URLS:
+ kwargs["base_url"] = PROVIDER_BASE_URLS[provider]
+ return backend, kwargs
+
+
+class ChatModelExtractor:
+ """Extracts connected AI_CHAT_MODEL_TYPES nodes as RLM other_backends.
+
+ Chat model nodes connected to input-tools provide small LMs for
+ llm_query()/rlm_query() at depth>=1.
+ """
+
+ @staticmethod
+ async def extract(tool_data: Optional[List[Dict[str, Any]]], auth) -> Tuple[List[str], List[Dict]]:
+ from constants import AI_CHAT_MODEL_TYPES
+ backends, kwargs_list = [], []
+
+ if not tool_data:
+ return backends, kwargs_list
+
+ for tool_info in tool_data:
+ node_type = tool_info.get('node_type', '')
+ if node_type not in AI_CHAT_MODEL_TYPES:
+ continue
+
+ params = tool_info.get('parameters', {})
+ provider = params.get('provider', '')
+ model = params.get('model', '')
+ api_key = params.get('api_key') or params.get('apiKey')
+
+ if not api_key and auth:
+ api_key = await auth.get_api_key(provider)
+ if not api_key:
+ logger.warning(f"[RLM] Skipping chat model node {node_type}: no API key")
+ continue
+
+ backend, kwargs = BackendAdapter.adapt(provider, model, api_key)
+ backends.append(backend)
+ kwargs_list.append(kwargs)
+ logger.info(f"[RLM] Extracted small LM: {provider}/{model} -> {backend}")
+ break # RLM currently supports one other_backend
+
+ return backends, kwargs_list
+
+
+class ToolBridgeAdapter:
+ """Bridges MachinaOS tool nodes into RLM custom_tools dict.
+
+ Creates sync callable wrappers that route through execute_tool() dispatcher.
+ Uses asyncio.run_coroutine_threadsafe() to bridge async handlers into
+ RLM's synchronous exec() REPL thread.
+ """
+
+ # Brief descriptions for common tool types (used in RLM REPL context)
+ TOOL_DESCRIPTIONS = {
+ 'calculatorTool': 'Math operations: add, subtract, multiply, divide, power, sqrt, mod, abs. Args: operation, a, b',
+ 'currentTimeTool': 'Get current date/time. Args: timezone (optional)',
+ 'duckduckgoSearch': 'Web search via DuckDuckGo. Args: query, max_results (optional)',
+ 'pythonExecutor': 'Execute Python code. Args: code (must set output variable)',
+ 'httpRequest': 'HTTP request. Args: url, method (GET/POST/PUT/DELETE), body (optional)',
+ 'httpRequestTool': 'HTTP request. Args: url, method (GET/POST/PUT/DELETE), body (optional)',
+ 'braveSearch': 'Web search via Brave. Args: query',
+ 'serperSearch': 'Web search via Google/Serper. Args: query',
+ 'perplexitySearch': 'AI-powered web search via Perplexity. Args: query',
+ 'crawleeScraper': 'Read/extract content from web pages. Args: url',
+ 'gmail': 'Send/search/read emails. Args: operation, ...',
+ 'calendar': 'Manage Google Calendar events. Args: operation, ...',
+ 'drive': 'Manage Google Drive files. Args: operation, ...',
+ 'sheets': 'Read/write Google Sheets. Args: operation, ...',
+ 'tasks': 'Manage Google Tasks. Args: operation, ...',
+ 'contacts': 'Manage Google Contacts. Args: operation, ...',
+ 'taskManager': 'Track delegated tasks. Args: operation',
+ 'timer': 'Wait for duration. Args: duration, unit',
+ }
+
+ @staticmethod
+ def bridge(tool_data: Optional[List[Dict[str, Any]]], context: Optional[Dict] = None) -> Dict[str, Dict]:
+ from constants import AI_AGENT_TYPES, AI_CHAT_MODEL_TYPES
+ from services.handlers.tools import execute_tool
+
+ if not tool_data:
+ return {}
+
+ main_loop = asyncio.get_event_loop()
+ tools = {}
+
+ for tool_info in tool_data:
+ node_type = tool_info.get('node_type', '')
+
+ # Skip agents (delegation) and chat models (handled by ChatModelExtractor)
+ if node_type in AI_AGENT_TYPES or node_type in AI_CHAT_MODEL_TYPES:
+ continue
+
+ node_id = tool_info.get('node_id', '')
+ label = tool_info.get('label', node_type)
+ params = tool_info.get('parameters', {})
+
+ def _make_sync_wrapper(t_type, t_id, t_params, t_label):
+ def wrapper(**kwargs):
+ config = {
+ 'node_type': t_type,
+ 'node_id': t_id,
+ 'parameters': t_params,
+ 'label': t_label,
+ }
+ if context:
+ config['nodes'] = context.get('nodes', [])
+ config['edges'] = context.get('edges', [])
+ future = asyncio.run_coroutine_threadsafe(
+ execute_tool(t_label, kwargs, config), main_loop,
+ )
+ return future.result(timeout=60)
+ return wrapper
+
+ # Clean tool name (same pattern as _build_tool_from_node ai.py:2567)
+ tool_name = re.sub(r'[^a-zA-Z0-9_]', '_', label.lower().replace(' ', '_'))
+ description = ToolBridgeAdapter.TOOL_DESCRIPTIONS.get(
+ node_type, f"Execute {label} ({node_type})"
+ )
+
+ tools[tool_name] = {
+ "tool": _make_sync_wrapper(node_type, node_id, params, label),
+ "description": description,
+ }
+ logger.info(f"[RLM] Bridged tool: {tool_name} ({node_type})")
+
+ return tools
diff --git a/server/services/rlm/constants.py b/server/services/rlm/constants.py
new file mode 100644
index 00000000..b7d7958c
--- /dev/null
+++ b/server/services/rlm/constants.py
@@ -0,0 +1,23 @@
+"""RLM service constants."""
+
+# MachinaOS provider -> RLM backend mapping
+PROVIDER_TO_BACKEND = {
+ 'openai': 'openai',
+ 'anthropic': 'anthropic',
+ 'gemini': 'gemini',
+ 'groq': 'openai',
+ 'openrouter': 'openrouter',
+ 'cerebras': 'openai',
+}
+
+# Base URLs for OpenAI-compatible providers
+PROVIDER_BASE_URLS = {
+ 'groq': 'https://api.groq.com/openai/v1',
+ 'openrouter': 'https://openrouter.ai/api/v1',
+ 'cerebras': 'https://api.cerebras.ai/v1',
+}
+
+# Default RLM execution parameters
+DEFAULT_MAX_ITERATIONS = 30
+DEFAULT_MAX_DEPTH = 1
+DEFAULT_VERBOSE = False
diff --git a/server/services/rlm/service.py b/server/services/rlm/service.py
new file mode 100644
index 00000000..9babd0a9
--- /dev/null
+++ b/server/services/rlm/service.py
@@ -0,0 +1,187 @@
+"""RLM execution service.
+
+Follows the same interface as AIService.execute_agent() / execute_chat_agent()
+but replaces LangGraph with RLM's REPL-based execution loop.
+"""
+
+import asyncio
+import time
+from typing import Dict, Any, List, Optional
+
+from core.logging import get_logger
+from .adapters import BackendAdapter, ChatModelExtractor, ToolBridgeAdapter
+from .constants import DEFAULT_MAX_ITERATIONS, DEFAULT_MAX_DEPTH, DEFAULT_VERBOSE
+
+logger = get_logger(__name__)
+
+
+class RLMService:
+ """Dedicated service for RLM agent execution.
+
+ Injected into AIService via dependency injection,
+ following the same pattern as MapsService, TextService, etc.
+ """
+
+ def __init__(self, auth=None):
+ self.auth = auth
+
+ async def execute(
+ self,
+ node_id: str,
+ parameters: Dict[str, Any],
+ memory_data: Optional[Dict[str, Any]] = None,
+ skill_data: Optional[List[Dict[str, Any]]] = None,
+ tool_data: Optional[List[Dict[str, Any]]] = None,
+ broadcaster=None,
+ workflow_id: Optional[str] = None,
+ context: Optional[Dict[str, Any]] = None,
+ database=None,
+ ) -> Dict[str, Any]:
+ """Execute RLM agent completion.
+
+ Same interface as AIService.execute_agent() / execute_chat_agent().
+ Reuses existing MachinaOS utilities for shared concerns.
+ """
+ start_time = time.time()
+
+ async def broadcast_status(phase: str, details: Dict[str, Any] = None):
+ if broadcaster:
+ await broadcaster.update_node_status(node_id, "executing", {
+ "phase": phase, "agent_type": "rlm", **(details or {})
+ }, workflow_id=workflow_id)
+
+ try:
+ # === Parameter extraction (same pattern as execute_chat_agent) ===
+ prompt = parameters.get('prompt', '')
+ system_message = parameters.get('systemMessage', '')
+
+ # Skill injection (reuse ai.py:833 _build_skill_system_prompt)
+ from services.ai import _build_skill_system_prompt
+ skill_prompt, has_personality = _build_skill_system_prompt(
+ skill_data, log_prefix="[RLM]"
+ )
+ if skill_prompt:
+ system_message = skill_prompt if has_personality else (
+ f"{system_message}\n\n{skill_prompt}" if system_message else skill_prompt
+ )
+
+ # Options flattening (same as execute_chat_agent line 1995)
+ options = parameters.get('options', {})
+ flattened = {**parameters, **options}
+
+ # Provider/model/API key resolution (same as lines 1998-2014)
+ from services.ai import is_model_valid_for_provider, get_default_model_async
+ api_key = flattened.get('api_key') or flattened.get('apiKey')
+ provider = parameters.get('provider', 'openai')
+ model = parameters.get('model', '')
+
+ if not model or not is_model_valid_for_provider(model, provider):
+ model = await get_default_model_async(provider, database)
+
+ if not api_key and self.auth:
+ api_key = await self.auth.get_api_key(provider)
+ if not api_key:
+ raise ValueError(f"API key required for RLM Agent (provider: {provider})")
+
+ await broadcast_status("initializing", {
+ "message": f"Initializing RLM with {provider}/{model}",
+ "provider": provider, "model": model,
+ })
+
+ # === Memory injection ===
+ if memory_data and memory_data.get('memory_content'):
+ memory_text = memory_data['memory_content']
+ system_message = (system_message or "") + f"\n\n## Conversation History\n{memory_text}"
+
+ # === Adapter: Map provider to RLM backend ===
+ backend, backend_kwargs = BackendAdapter.adapt(provider, model, api_key)
+
+ # === Adapter: Extract chat model nodes as other_backends ===
+ other_backends, other_backend_kwargs = await ChatModelExtractor.extract(
+ tool_data, self.auth
+ )
+
+ # === Adapter: Bridge tool nodes as RLM custom_tools ===
+ await broadcast_status("building_tools", {
+ "message": "Bridging connected tools..."
+ })
+ custom_tools = ToolBridgeAdapter.bridge(tool_data, context)
+
+ # === RLM-specific parameters ===
+ max_iterations = int(flattened.get('maxIterations', DEFAULT_MAX_ITERATIONS))
+ max_depth = int(flattened.get('maxDepth', DEFAULT_MAX_DEPTH))
+ max_budget = flattened.get('maxBudget')
+ max_timeout = flattened.get('maxTimeout')
+ max_tokens = flattened.get('maxTokens')
+ verbose = flattened.get('verbose', DEFAULT_VERBOSE)
+
+ # === Run RLM in thread pool (RLM is synchronous) ===
+ await broadcast_status("executing", {
+ "message": f"RLM executing (max {max_iterations} iterations)...",
+ "max_iterations": max_iterations, "max_depth": max_depth,
+ "tool_count": len(custom_tools),
+ })
+
+ def _run_rlm():
+ from rlm import RLM
+ from rlm.logger import RLMLogger
+ rlm_instance = RLM(
+ backend=backend, backend_kwargs=backend_kwargs,
+ environment="local",
+ other_backends=other_backends if other_backends else None,
+ other_backend_kwargs=other_backend_kwargs if other_backend_kwargs else None,
+ max_iterations=max_iterations, max_depth=max_depth,
+ max_budget=float(max_budget) if max_budget else None,
+ max_timeout=float(max_timeout) if max_timeout else None,
+ max_tokens=int(max_tokens) if max_tokens else None,
+ custom_system_prompt=system_message if system_message else None,
+ custom_tools=custom_tools if custom_tools else None,
+ logger=RLMLogger(), verbose=verbose,
+ )
+ return rlm_instance.completion(prompt)
+
+ result = await asyncio.to_thread(_run_rlm)
+
+ # === Memory save (same pattern as execute_chat_agent) ===
+ if memory_data and memory_data.get('node_id'):
+ from services.memory_store import save_message
+ session_id = memory_data.get('session_id', 'default')
+ save_message(session_id, "user", prompt)
+ save_message(session_id, "assistant", result.response)
+
+ execution_time = time.time() - start_time
+ iterations = len(result.metadata.get("iterations", [])) if result.metadata else 0
+
+ await broadcast_status("completed", {
+ "message": f"RLM completed in {execution_time:.1f}s",
+ "iterations": iterations,
+ })
+
+ return {
+ "success": True,
+ "node_id": node_id,
+ "node_type": "rlm_agent",
+ "result": {
+ "response": result.response,
+ "model": model,
+ "provider": provider,
+ "total_cost": result.usage_summary.total_cost,
+ "total_input_tokens": result.usage_summary.total_input_tokens,
+ "total_output_tokens": result.usage_summary.total_output_tokens,
+ "execution_time": execution_time,
+ "iterations": iterations,
+ },
+ "execution_time": execution_time,
+ }
+
+ except Exception as e:
+ logger.error(f"[RLM] Execution failed: {e}", exc_info=True)
+ execution_time = time.time() - start_time
+ await broadcast_status("error", {"message": str(e)})
+ return {
+ "success": False,
+ "node_id": node_id,
+ "node_type": "rlm_agent",
+ "error": str(e),
+ "execution_time": execution_time,
+ }
diff --git a/server/skills/rlm_agent/rlm-reasoning-skill/SKILL.md b/server/skills/rlm_agent/rlm-reasoning-skill/SKILL.md
new file mode 100644
index 00000000..bf44c245
--- /dev/null
+++ b/server/skills/rlm_agent/rlm-reasoning-skill/SKILL.md
@@ -0,0 +1,120 @@
+---
+name: rlm-reasoning-skill
+description: Guides the RLM agent to use REPL code execution with recursive LM calls for complex reasoning, decomposition, and multi-step problem solving.
+metadata:
+ author: machina
+ version: "1.0"
+ category: reasoning
+ icon: "brain"
+ color: "#FF8C00"
+---
+
+# RLM Recursive Reasoning Skill
+
+You are an RLM (Recursive Language Model) agent. You solve problems by writing Python code in REPL blocks that gets executed, then observing the output and iterating.
+
+## Core Workflow
+
+1. Write code inside triple-backtick `repl` blocks to execute Python
+2. Observe stdout from execution, then write more code blocks as needed
+3. Signal your final answer with `FINAL(answer)` or `FINAL_VAR(variable_name)`
+
+## REPL Code Blocks
+
+Write executable Python inside fenced code blocks with the `repl` language tag:
+
+```
+```repl
+# Your Python code here
+result = 2 + 2
+print(result)
+```
+```
+
+The code runs via `exec()` in a sandboxed Python environment. Variables persist across iterations within the same session.
+
+## Available Functions
+
+| Function | Purpose |
+|----------|---------|
+| `llm_query(prompt)` | Call a smaller LM for sub-tasks (summarization, extraction, classification) |
+| `rlm_query(prompt)` | Spawn a recursive child RLM with its own REPL for complex sub-problems |
+| `FINAL(answer)` | Signal completion with a direct answer string |
+| `FINAL_VAR(var_name)` | Signal completion using the value of a variable in the REPL namespace |
+| `SHOW_VARS()` | Print all current variables in the REPL namespace |
+| `print()` | Standard output -- you will see this in the next iteration |
+
+## The `context` Variable
+
+The user's input is stored as a Python variable called `context` in the REPL namespace. Access it directly in your code:
+
+```
+```repl
+# Access the user's input
+print(context)
+print(len(context))
+```
+```
+
+**Important**: The context is never sent to the LM directly. You must use code to examine, process, and extract information from it.
+
+## When to Use `llm_query()` vs `rlm_query()`
+
+- **`llm_query(prompt)`**: Simple sub-tasks that need one LM call. Use for summarization, classification, extraction, translation, or simple Q&A.
+- **`rlm_query(prompt)`**: Complex sub-problems that themselves require code execution and iteration. Use when the sub-task needs its own REPL loop.
+
+## Signaling Completion
+
+Always end with exactly one of:
+
+```
+```repl
+FINAL("Your final answer here")
+```
+```
+
+Or if the answer is in a variable:
+
+```
+```repl
+FINAL_VAR("result")
+```
+```
+
+## Problem-Solving Strategies
+
+1. **Decompose first**: Break complex problems into smaller sub-problems
+2. **Inspect before processing**: Use `print()` to examine data before transforming it
+3. **Iterate incrementally**: Solve one piece at a time, verify each step
+4. **Use `llm_query()` for language tasks**: Summarization, extraction, reasoning about text
+5. **Use `rlm_query()` for complex sub-problems**: When a sub-task needs its own code execution loop
+6. **Accumulate results**: Store intermediate results in variables, combine at the end
+
+## Example Pattern
+
+```
+```repl
+# Step 1: Examine the input
+print(f"Context length: {len(context)}")
+print(context[:200])
+```
+```
+
+Observe output, then:
+
+```
+```repl
+# Step 2: Process with a sub-query
+summary = llm_query(f"Summarize this text in 3 bullet points: {context[:1000]}")
+print(summary)
+```
+```
+
+Observe output, then:
+
+```
+```repl
+# Step 3: Final answer
+FINAL(summary)
+```
+```