Message Processing Logic & XML Tags
Understanding how LeClerk processes messages internally is crucial for developers working on the chatbot. This page explains the sophisticated message processing pipeline and the XML tag system that makes it work.The Internal Processing Pipeline
When the agent receives a message to process, it goes through several stages:XML Tag System
LeClerk uses XML tags to structure the language model’s responses. This allows the LLM to think through problems, take notes, and separate what gets sent to customers from internal processing.Customer-Facing Tags
<response_for_customer>
This is the only tag that gets sent to the customer. Everything else is stripped away.
Internal Processing Tags
<thinking>
The LLM uses this to reason through problems step by step. This content is never shown to customers but helps the model make better decisions.
<scratchpad>
Used for temporary notes, calculations, or working through complex scenarios.
<nexus_instructions>
Special instructions from system events that guide the LLM’s response. These are injected by LeClerk when processing events like payment confirmations or delivery updates.
Message Processing Flow
Here’s how a typical message gets processed:1. Prompt Construction
The ChatAgent builds a comprehensive prompt that includes:- System instructions and personality guidelines
- Restaurant information (hours, menu highlights, policies)
- Customer context (history, preferences, current orders)
- Conversation history
- Available tools and when to use them
2. LLM Processing
The language model receives this rich context and generates a response using the XML structure:3. XML Parsing and Extraction
LeClerk parses the response and:- Extracts the
<response_for_customer>content - Logs internal tags for debugging and monitoring
- Processes any special instructions
- Validates the response format
4. Tool Call Integration
If the LLM requested tool calls during processing, they’re executed, the response is stored in the conversation and the LLM is runned againMulti-turn Tool Usage
Complex scenarios might require multiple tool calls:Debugging Message Processing
Common Issues
Missing XML TagsMonitoring Tools
- Langfuse Traces: See full conversation flows with timing
- Response Logs: Review XML structure and content
- Error Alerts: Get notified when parsing fails
- Quality Metrics: Track response appropriateness and success rates
Best Practices for Developers
- Always Use XML Structure: Never modify prompts to skip XML formatting
- Keep Internal Tags Clean: Don’t put customer-facing content in thinking/scratchpad
- Test Edge Cases: What happens with very long responses? Empty responses?
- Monitor Response Quality: Regularly review logs to catch issues early
- Validate Changes: Test prompt modifications thoroughly before deployment