Overview
The agent workflow consists of three primary nodes that form a continuous cycle:Workflow Graph
The LangGraph state machine is defined as follows:State Structure
The agent maintains state across the workflow:past_steps field uses operator.add to automatically accumulate results across iterations.
See plan_execute_agent/rdkit_agent.py:75 for state definition.
Node 1: Planner
Purpose
The planner analyzes the input query and generates a structured execution plan.Implementation
Prompt Structure
The planner uses a specialized prompt to ensure chemistry-specific planning:Structured Output
The planner returns a Pydantic model for type safety:Example Plan
For the query:"What is the SMILES for <IUPAC> aspirin </IUPAC>?"
The planner generates:
Node 2: Executor (Agent)
Purpose
Executes the first step of the current plan using the appropriate tool.Implementation
Tool Selection
The executor (powered by GPT-4o) autonomously selects which tool to use based on the task description:Execution Example
Task: “Call ‘structure_chem_prompt’ to tag chemical information” Agent reasoning:Node 3: Replanner
Purpose
Evaluates completed steps and decides whether to:- End: Return final response to user
- Continue: Generate new plan and execute next step
Implementation
Decision Models
The replanner uses union types to represent decisions:Replanner Prompt
The replanner receives full context of the conversation:Handling Validation Errors
The replanner is crucial for handling SMILES validation failures:Error Detection
Whenvalidate_smiles_rdkit returns invalid SMILES:
Replanning Strategy
The replanner analyzes the error and creates a corrective plan: Example:Recursion Limit and Attempt Tracking
Preventing Infinite Loops
The system includes safeguards against infinite replanning:Recursion Error Handling
When the limit is exceeded, the system catches the error:Tracking Metrics
The system logs attempt statistics for analysis:Complete Workflow Example
Let’s trace a full execution for:"What is the molecular formula of aspirin?"
Iteration 1
Planner Output:structure_chem_prompt("What is the molecular formula of aspirin?")
Result: {"new_prompt": "What is the molecular formula of <IUPAC> aspirin </IUPAC>?"}
Replanner Decision: Continue with remaining steps
Iteration 2
Replanner Output:answer_chemistry_query("What is the molecular formula of <IUPAC> aspirin </IUPAC>?")
Result: "<MOLFORMULA> C9H8O4 </MOLFORMULA>"
Replanner Decision: Continue to validation
Iteration 3
Replanner Output:Iteration 4
Replanner Output:should_end() returns END
Final: Return to user
Low VRAM Mode
WhenLOW_VRAM=True in configuration:
- Respond directly without using LlaSMol
- Use only GPT-4o for general chemistry questions
- Return early with available information
Best Practices
Optimal Planning
Optimal Planning
- Keep plans simple (3-4 steps maximum)
- Ensure each step has clear success criteria
- Include validation steps for SMILES outputs
Error Recovery
Error Recovery
- Use validity vectors to identify specific errors
- Limit replanning attempts to avoid infinite loops
- Provide clear error messages to the replanner
Performance
Performance
- Enable RAG only when additional context is needed
- Monitor
replanning_attemptsto identify problematic queries - Use LOW_VRAM mode for CPU-only deployments
Next Steps
Architecture
Learn about the system architecture
Chemistry Tags
Master the tag system for queries