---
id: agent-orchestration
name: Chat Agent Orchestration
version: 1.0.0
summary: >-
  A customer submits a conversational request through Storefront; Chat API
  screens input, routes via a non-LLM classifier with optional human
  disambiguation, delegates to specialist agents, and formats the response.
owners:
  - ai-capabilities-team
badges:
  - content: AI Agent Orchestration
    backgroundColor: purple
    textColor: purple
steps:
  - id: CustomerSubmitsChat
    title: Customer sends a chat message
    summary: >-
      Submits a book-discovery, review-summary, or store-policy question through
      the Storefront chat interface.
    actor:
      name: Customer
    next_step:
      id: InvokeChatWorkflow
      label: Send authenticated chat request
  - id: InvokeChatWorkflow
    title: Chat API starts conversational workflow
    summary: >-
      Enforces customer authentication and per-user rate limits, loads session
      state, and starts the composed conversational workflow.
    service:
      id: chat-api
      version: 1.0.0
    next_steps:
      - id: SessionStore
        label: Load or persist conversation session
      - id: InputScreening
        label: Screen and normalize customer input
  - id: SessionStore
    title: Chat Session Blob Store
    summary: Persists multi-turn conversation history in Azure Blob Storage.
    container:
      id: chat-session-blob
      version: 1.0.0
  - id: InputScreening
    title: Input Safety & Length Screening
    summary: >-
      Trims whitespace, rejects empty messages and prompt-injection patterns,
      truncates prompts over 2,000 characters, and redacts PII via Data Privacy.
    service:
      id: presidio-api
      version: 1.0.0
    next_steps:
      - id: DecisionRouter
        label: Accepted input
      - id: RejectedBoundary
        label: Empty or unsafe input rejected
  - id: RejectedBoundary
    title: Boundary rejection response
    summary: Returns a safe rejection message without invoking routing or AI agents.
    custom:
      title: Input rejected
      type: Outcome
      icon: ExclamationTriangleIcon
      color: red
  - id: DecisionRouter
    title: Non-LLM Intent & Scope Router
    summary: >-
      Evaluates English language, BookWorm scope, and target specialist using a
      lightweight decision model. Low-confidence in-scope requests trigger
      customer disambiguation.
    custom:
      title: Decision Router
      type: Router
      icon: AdjustmentsHorizontalIcon
      color: blue
    next_steps:
      - id: RouteDisambiguation
        label: Low-confidence in-scope route
      - id: BookAgent
        label: Book discovery or general reviews
      - id: QAAgent
        label: Store policies and support
      - id: RatingAgent
        label: Review summary with book ID
      - id: OutOfScopeBoundary
        label: Non-English or out-of-scope
  - id: RouteDisambiguation
    title: Customer Route Disambiguation
    summary: >-
      Pauses the workflow and prompts the customer to choose between Book, QA,
      or Rating specialists before resuming.
    actor:
      name: Customer
    next_steps:
      - id: BookAgent
        label: Selected Book Agent
      - id: QAAgent
        label: Selected QA Agent
      - id: RatingAgent
        label: Selected Rating Agent
      - id: OutOfScopeBoundary
        label: Unresolved or out-of-scope
  - id: OutOfScopeBoundary
    title: Scope boundary response
    summary: >-
      Returns a polite out-of-scope or language boundary message without
      invoking specialist agents.
    custom:
      title: Out-of-scope boundary
      type: Outcome
      icon: CheckCircleIcon
      color: gray
  - id: BookAgent
    title: Book Agent
    summary: >-
      Handles catalog search and recommendations using MCP Tools, or delegates
      complex multi-part discovery to parallel sub-researchers.
    agent:
      id: book-agent
      version: 1.0.0
    next_steps:
      - id: MCPTools
        label: Direct catalog lookup
      - id: ParallelBookResearch
        label: Complex multi-part discovery
      - id: RatingAgent
        label: Hand off review summary
      - id: QAAgent
        label: Hand off store policy question
  - id: MCPTools
    title: MCP Server - Catalog Tools
    summary: Provides read-only book, author, and category tools to Book specialists.
    service:
      id: mcp-server
      version: 1.0.0
    next_step:
      id: ResponseFormatter
      label: Return grounded catalog response
  - id: QAAgent
    title: QA Agent
    summary: >-
      Answers store-policy, shipping, and returns questions from grounded policy
      context, directing live account requests to human support.
    agent:
      id: qa-agent
      version: 1.0.0
    next_steps:
      - id: BookAgent
        label: Follow-up needs book facts
      - id: ResponseFormatter
        label: Customer-support response ready
  - id: RatingAgent
    title: Rating Agent
    summary: >-
      Summarizes book reviews and rating signals over Agent-to-Agent gRPC in
      Rating API.
    agent:
      id: rating-agent
      version: 1.0.0
    next_step:
      id: ResponseFormatter
      label: Return review summary
  - id: ParallelBookResearch
    title: Parallel Book Research
    summary: Runs factual catalog lookup and preference recommendation in parallel.
    custom:
      title: Parallel Research Fan-Out
      type: Orchestration
      icon: ArrowsPointingOutIcon
      color: purple
    next_steps:
      - id: BookCatalogResearcher
        label: Book facts and metadata
      - id: BookRecommendationResearcher
        label: Preference-matched shortlist
  - id: BookCatalogResearcher
    title: Book Catalog Researcher
    summary: Gathers concise facts about books, authors, categories, and publishers.
    agent:
      id: book-catalog-researcher
      version: 1.0.0
    next_step:
      id: SynthesizeBookResearch
      label: Return catalog findings
  - id: BookRecommendationResearcher
    title: Book Recommendation Researcher
    summary: Curates a preference-matched shortlist with supporting facts.
    agent:
      id: book-recommendation-researcher
      version: 1.0.0
    next_step:
      id: SynthesizeBookResearch
      label: Return recommendation shortlist
  - id: SynthesizeBookResearch
    title: Book Agent synthesizes parallel research
    summary: Merges findings, deduplicates titles, and composes a unified answer.
    agent:
      id: book-agent
      version: 1.0.0
    next_step:
      id: ResponseFormatter
      label: Format final response
  - id: ResponseFormatter
    title: Response Hygiene & Formatting
    summary: >-
      Trims assistant output, strips accidental system-prompt fragments, and
      normalizes punctuation before returning to Storefront.
    service:
      id: chat-api
      version: 1.0.0
---

## Trigger & Business Outcome

This flow is triggered when an authenticated customer sends a conversational message from the Storefront to [[service|chat-api]]. Success is confirmed when [[service|chat-api]] screens the input, routes it to the appropriate specialist ([[agent|book-agent]], [[agent|qa-agent]], or [[agent|rating-agent]]), persists session context in [[container|chat-session-blob]], and returns a formatted, read-only response without exposing transactional order or account mutations.

## Interactive Flow Visualization

<NodeGraph />

## Step-by-Step Walkthrough

<Steps title="Guarded Conversational Routing and Specialist Execution">
	<Step title="Authenticate and screen customer input">
		[[service|chat-api]] enforces customer authentication and per-user rate limits, loads multi-turn
		history from [[container|chat-session-blob]], and screens input for empty prompts, prompt
		injection, excessive length, and PII redaction via [[service|presidio-api]].
	</Step>
	<Step title="Classify scope and specialist route">
		A lightweight non-LLM decision router checks English language, BookWorm scope, and target
		specialist. Non-English or off-topic prompts short-circuit immediately; low-confidence in-scope
		prompts pause for customer disambiguation.
	</Step>
	<Step title="Execute specialist or parallel research">
		[[agent|book-agent]] queries live catalog data through [[service|mcp-server]] or fans out
		concurrently to [[agent|book-catalog-researcher]] and [[agent|book-recommendation-researcher]].
		[[agent|qa-agent]] answers store-policy questions, and [[agent|rating-agent]] provides review
		summaries over Agent-to-Agent gRPC.
	</Step>
	<Step title="Normalize and return response">
		[[service|chat-api]] strips any leaked system-prompt fragments, normalizes punctuation, saves
		updated session state in [[container|chat-session-blob]], and streams the final answer to
		Storefront.
	</Step>
</Steps>

## Failure, Retry & Compensation Behavior

| Failure Point                              | System Behavior                                                                                       | Compensation / Recovery                                                |
| :----------------------------------------- | :---------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------- |
| **Prompt injection or empty input**        | Input screening rejects the turn before routing                                                       | Returns a safe boundary prompt without consuming specialist LLM tokens |
| **Low-confidence in-scope route**          | Workflow pauses and asks the customer to pick a specialist                                            | Resumes immediately with the customer's chosen specialist              |
| **Account or live-order mutation request** | <ResourceLink id="qa-agent" type="agent">qa-agent</ResourceLink> enforces a strict read-only boundary | Directs the customer to Commerce or human support                      |

:::warning
**Read-only conversational boundary:** None of the conversational agents can edit baskets, place orders, cancel orders, or submit reviews. Always route customer traffic through [[service|chat-api]] so input screening, PII redaction, and response formatting are enforced.
:::

## Participating Resources

- **Domains & Systems:** [[domain|conversational]], [[domain|reviews]], [[system|chat-system]], [[system|mcp-tools]], [[system|data-privacy]]
- **Services & Stores:** [[service|chat-api]], [[service|mcp-server]], [[service|presidio-api]], [[container|chat-session-blob]]
- **AI Agents:** [[agent|book-agent]], [[agent|book-catalog-researcher]], [[agent|book-recommendation-researcher]], [[agent|qa-agent]], [[agent|rating-agent]]
- **Related Views:** [[flow|feedback-publication]]
