LLM Orchestration Service
The LLMOrchestrationService serves as a bridge between the AgentNode and CoreLLM, adding orchestration-specific functionality to LLM interactions.
Core Responsibilities
- Stream Management: Wraps CoreLLM's streaming capabilities with orchestration state
- Token Usage Tracking: Updates token usage in session state
- Tool Tracking: Monitors and records tools used during interactions
- Callback Injection: Provides orchestration-aware callbacks for stream events
Architecture Position
Key Methods
streamWithOrchestration
The primary method that wraps CoreLLM.streamText with orchestration capabilities:
async streamWithOrchestration(
options: StreamWithOrchestrationOptions
): Promise<AgentDockStreamResult<Record<string, CoreTool>, any>>
This method:
- Prepares orchestration-aware callbacks for
onFinishandonStepFinish - Calls
CoreLLM.streamTextwith these callbacks - Returns an enhanced stream result with orchestration state
updateTokenUsage
A private method that updates the token usage in the session state:
private async updateTokenUsage(usage?: TokenUsage): Promise<void>
This method:
- Fetches the current state from the
OrchestrationManager - Updates the cumulative token usage with the new usage information
- Stores the updated state back in the session
Integration with Tool Tracking
The service also tracks tools used during the conversation:
- Monitors tool calls in the
onStepFinishcallback - Updates the
recentlyUsedToolsarray in session state - Provides this information to orchestration rules for conditional transitions
Constructor
constructor(
private llm: CoreLLM,
private orchestrationManager: OrchestrationManager,
private sessionId: SessionId
)
The service requires:
- A
CoreLLMinstance for LLM interactions - An
OrchestrationManagerfor state management - A
sessionIdto identify the session context
Related Documentation
- Orchestration Overview - General orchestration concepts
- State Management - How state is managed in orchestration
- Response Streaming - Details on streaming capabilities
Step Activation
- Step Activation: Based on conditions met (e.g., a specific tool was used), the active step can change, altering the agent's behavior and available tools for the next turn.
- The system now re-evaluates conditions immediately after a tool is used, allowing for step transitions within the same turn a defining sequence is completed.
Example Scenario: Cognitive Reasoner Agent
Let's illustrate with the Cognitive Reasoner agent, specifically its EvaluationMode.
Agent Configuration (template.json excerpt):
{
"name": "EvaluationMode",
"description": "Critical evaluation sequence",
"sequence": [
"critique",
"debate",
"reflect"
],
"conditions": [
{ "type": "sequence_match" }
],
"availableTools": {
"allowed": ["critique", "debate", "reflect", "search"]
}
}
Flow:
- Initial State: Session starts,
activeStepisDefaultMode,recentlyUsedToolsis[]. - User Request: "Critique the argument that remote work improves productivity."
- LLM Action (Turn 1 - Critique): The agent, likely in
DefaultMode, uses thecritiquetool.processToolUsageis called.-
recentlyUsedToolsbecomes["critique"]. -
getActiveStepruns immediately. No sequence matches yet.activeStepremainsDefaultMode.
-
- LLM Action (Turn 2 - Debate): Following the critique, the agent uses the
debatetool (perhaps prompted internally or by user).processToolUsageis called.-
recentlyUsedToolsbecomes["critique", "debate"]. -
getActiveStepruns. No sequence matches yet.activeStepremainsDefaultMode.
-
- LLM Action (Turn 3 - Reflect): The agent uses the
reflecttool.processToolUsageis called.-
recentlyUsedToolsbecomes["critique", "debate", "reflect"]. -
getActiveStepruns. It checksEvaluationMode:- Condition
type: "sequence_match"is evaluated. - The end of
recentlyUsedTools["critique", "debate", "reflect"]matches the step'ssequence["critique", "debate", "reflect"]. - The condition passes.
- Condition
-
EvaluationModebecomes the newactiveStep. ThesequenceIndexis reset to0for this newly activated step.
-
- Next Turn: When the next interaction begins, the agent is now in
EvaluationMode. If the active step involved sequence enforcement via theStepSequencer, only the tool atsequenceIndex: 0(critique) would be initially allowed, although this specific example focuses on the transition viasequence_matchrather than sequence enforcement during the step.