Skip to content

Architecture

Architecture Overview

AI Agent Studio is built on a modular, plugin-based architecture using Strategy, Factory, and Chain of Responsibility patterns. The framework separates concerns across orchestration, execution, memory, security, and observability layers.

Core Components

Orchestrators

Four execution patterns: Conversational (multi-turn chat), Function (single-task with sync/async routing), Workflow (multi-agent state machines), Email (thread processing with auto-reply).

All extend BaseAgentOrchestrator and implement IAgentOrchestrator interface.

LLM Providers

Multi-provider support: OpenAI, Claude (addon), Gemini (addon). Extensible via ILLMProviderAdapter interface.

BaseProviderAdapter provides common HTTP handling, retry logic, and error normalization.

Actions

Standard actions: GetRecordDetails, CreateRecord, UpdateRecord, FlowHandler, PostChatter.

Addon actions: RunReport, SearchKnowledge, SendNotification.

Extend BaseAgentAction implementing IAgentAction interface.

Context Providers

Dynamic data enrichment: Supply related records, user context, and computed data to agents.

Implement IAgentContextProvider interface with bulk-safe, security-enforced queries.

Execution Lifecycle

  1. Request Entry

    Request enters via AgentExecutionService.startExecution() (invocable from Flow/Apex) or REST API endpoint /services/apexrest/ai/agent/process/.

  2. Orchestrator Routing

    AgentExecutionService.getOrchestrator() uses AgentOrchestratorMapping__mdt to route by agent type to ConversationalOrchestrator, FunctionOrchestrator, WorkflowOrchestrator, or EmailOrchestrator.

  3. Security Checks

    Prompt Safety: PromptSafetyService runs 3-layer detection (pattern-based, heuristic, structural) with configurable response modes (Block/Sanitize/Flag/LogOnly).

    PII Masking: PIIMaskingService applies hybrid masking (schema-based + pattern-based) with deterministic token replacement.

  4. Context Assembly

    Memory: ContextManagerService retrieves conversation history via IMemoryManager implementations (BufferWindow or SummaryBuffer).

    Dynamic Context: ContextResolverService invokes registered IAgentContextProvider implementations to gather related data.

    System Prompt: SystemPromptBuilder assembles identity, instructions, context, and orchestrator-specific additions.

  5. LLM Inference

    Request Building: LLMInteractionService.prepareAndCallLLM() formats messages and tools via LLMFormattingService.

    Tool Reasoning: If enabled, adds _rationale parameter to all tools for explainability.

    Provider Call: Routes to ILLMProviderAdapter implementation (OpenAIProviderAdapter, ClaudeProviderAdapter, GeminiProviderAdapter).

    Response Parsing: Normalizes provider-specific responses into ProviderResult.

  6. Response Processing

    OrchestrationService.processLlmResult() selects handler based on response type:

    • Tool Calls: ToolCallResponseHandler validates dependencies (if enabled), executes tools, manages async dispatch
    • Content Only: ContentResponseHandler processes text responses, handles email replies for EmailOrchestrator

    Three-Tier Error Policy: Capability FailFast → MaxRetries → Pass to LLM for recovery

  7. Action Execution

    CapabilityExecutionService routes to action implementations:

    • Standard: Maps via StandardActionHandler__mdt to framework actions
    • Apex: Instantiates custom Apex class via Type.forName()
    • Flow: Invokes Flow via Flow.Interview.createInterview()

    HITL: If configured, routes through HITLGatewayService for approval workflows.

    Security: TypeCoercionService.coerceArgumentTypesForSObject() enforces FLS on field access.

  8. State Management

    Execution Records: AgentStateService manages AgentExecution__c lifecycle with status tracking (Idle/Processing/Completed/Failed/Cancelled).

    Step Logging: ExecutionStepService creates ExecutionStep__c for detailed execution trace (UserInput/AgentResponse/ToolCall/ToolResult/Error).

    Decision Steps: AgentDecisionStepLogger creates AgentDecisionStep__c for UI storyboard display.

    Turn Tracking: CurrentTurnIdentifier__c enables stale execution detection via StaleJobDetector.

  9. Async Continuation

    Follow-up LLM: FollowUpLLMEngine handles multi-turn responses, supports deferred DML optimization.

    Async Actions: AsyncActionEngine executes long-running tools in separate queueable.

    Multi-LLM Optimization: TransactionContext tracks DML/callout operations to determine if immediate follow-up is possible.

  10. Completion

    Status Update: AgentStateService.updateStatus() commits buffered DML (if deferred mode enabled) and sets terminal state.

    Events: Publishes AgentResponse__e Platform Event for conversational agents.

    Workflow Callbacks: For child agents, triggers onChildComplete() on parent orchestrator.

Key Services

ServiceResponsibility
AgentExecutionServiceEntry point, orchestrator routing, service user context switching
OrchestrationServiceLLM response processing, handler selection (ToolCallResponseHandler vs ContentResponseHandler)
LLMInteractionServiceLLM communication, prompt building, provider adapter routing
AgentStateServiceExecution lifecycle management, status updates, Platform Event publishing
CapabilityExecutionServiceTool/action execution, handler factory (Standard/Apex/Flow)
ContextManagerServiceContext ledger, memory strategy selection (BufferWindow vs SummaryBuffer)
AIAgentConfigServiceConfiguration repository (agents, capabilities, context providers)
ExecutionStepServiceExecution step CRUD, deferred DML buffering
TypeCoercionServiceType conversion with FLS enforcement for SObject fields
AgentJobEnqueuerAsync job dispatch (Queueable vs Platform Event based on AsyncDispatchType__c)
AgentDecisionStepLoggerDecision logging for storyboard UI explainability
TransactionContextMulti-LLM optimization, deferred DML mode, LLM call tracking, tool safety detection
PromptSafetyServiceMulti-layer jailbreak detection with configurable response modes
PIIMaskingServiceHybrid PII masking (schema-based + pattern-based) with bidirectional unmask
ToolDependencyValidatorDeclarative tool sequencing constraint enforcement
AgentExecutionDispatcherDLQ-based dispatch for multi-record executions with BatchId correlation
AgentExecutionWorkerDLQ worker with self-pull pattern and FOR UPDATE locking
StaleJobDetectorScheduled recovery of stuck executions (checks ProcessingStartedAt__c)

Configuration Objects

Core agent configuration:

Identity & Behavior: AgentType__c (Conversational/Function/Workflow/Email), IdentityPrompt__c and InstructionsPrompt__c (System prompt components), EnableToolReasoning__c (Require LLM to explain tool selection), EnableParallelToolCalling__c (Allow multiple tools per response).

Memory & Context: LLMConfiguration__c (LLM provider settings lookup), MemoryStrategy__c (Buffer Window/Summary Buffer), HistoryTurnLimit__c (Number of turns to remember).

Security & Trust: PIIMaskingMode__c (Hybrid/Schema-Only/Pattern-Only), SensitiveClassifications__c (Data classifications to mask), PromptSafetyMode__c (Block/Sanitize/Flag/LogOnly), SafetyThreshold__c (Threat score threshold 0.0-1.0).

Performance: AsyncDispatchType__c (High for Platform Events / Low for Queueable), MaxProcessingCycles__c (Max LLM turns per execution), EnableDependencyValidation__c (Enforce tool dependency graph).

Workflow: ToolDependencyGraph__c (JSON dependency graph for validation).

Advanced Patterns

Multi-LLM Optimization (Deferred DML)

Enables multiple LLM calls within a single transaction when intervening tools don’t perform DML or callouts.

Flow: Entry enables deferred DML → ExecutionStepService buffers inserts → After each LLM call, check eligibility → If safe: continue in same transaction → On completion: commit all buffered DML

Eligibility: Deferred mode enabled + Pre-existing execution + No DML/callouts + Under max LLM calls limit

Benefits: Reduced latency (no queueable delays), fewer async jobs, faster UX

Dead Letter Queue Architecture

Multi-record executions use DLQ pattern for resilient processing at scale.

Flow: Dispatcher creates AgentExecution__c records with BatchId__c → Enqueue limited workers → Worker claims with FOR UPDATE → Process → On completion, trigger chains next pending record → Workers exhaust naturally

Features: Priority ordering (Priority__c DESC), exponential backoff (NextRetryAt__c), stale job recovery (StaleJobDetector), progress tracking (getBatchProgress)

Tool Dependency Validation

Declarative tool sequencing prevents workflow hallucinations.

Flow: LLM generates dependency graph → Admin approves → System enforces at runtime

Two-Phase: Pre-flight validation + topological sort → Runtime validation during execution

Circuit Breaker: Tracks violations, fails if threshold exceeded

Observability

Storyboard UI: AgentStoryboardController provides data for LWC components (agentStoryboard, agentExecutionView, workflowView, agentStoryboardStep)

Execution Trace: ExecutionStep__c records capture complete interaction history with token tracking

Decision Timeline: AgentDecisionStep__c provides user-friendly execution visualization

Tool Reasoning: When enabled, captures LLM rationale for tool selection for transparency

Cost Analytics: Token and cost tracking per step enables budget monitoring and optimization