---
id: rating-agent
name: Rating Agent
version: 1.0.0
summary: Read-only review intelligence specialist that summarizes customer feedback over A2A gRPC and map-reduce workflows while leaving final quality tier classification to deterministic service logic.
owners:
  - catalog-experience-team
repository:
  language: C#
  url: https://github.com/foxminchan/BookWorm
tools:
  - name: GetCustomerReviews
    type: function
    icon: /icons/languages/dotnet.svg
    description: Read-only in-process lookup returning raw customer reviews (review ID, rating, comment) for a book; rate-limited to 30 calls per minute.
  - name: get_book
    type: mcp
    icon: /icons/languages/dotnet.svg
    description: Retrieves catalog book metadata (title, authors, category, price, average rating, total reviews) from the MCP Server before fetching reviews.
sends:
  - id: AnalyzeText
    version: 1.0.0
  - id: AnonymizeText
    version: 1.0.0
readsFrom:
  - id: rating-database
    version: 1.0.0
flows:
  - id: agent-orchestration
    version: 1.0.0
badges:
  - content: AI Agent
    backgroundColor: purple
    textColor: purple
model:
  provider: Microsoft
  name: model-router
  version: "2025-11-18"
---

## Role & Autonomy Boundary

Rating Agent is the read-only review analysis and quality-signal specialist hosted inside [[service|rating-api]] in the Reviews bounded context. It is triggered through two runtime paths: remote Agent-to-Agent (A2A) gRPC handoffs from [[service|chat-api]] (routed or handed off from [[agent|book-agent]]), and [[service|rating-api]]'s in-process map-reduce summarization pipeline backing [[query|SummarizeFeedback]].

Rating Agent **never writes to [[container|rating-database]] or [[container|catalog-database]]**, never executes destructive queries, and never assigns final quality tiers—classifications (`No Data`, `Bad`, `Good`, `Top Rated`) are computed deterministically by service code rather than the language model.

## Agent Profile & Model Governance

| Property                     | Value                                                                                                                                                          |
| :--------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Bounded Context / Domain** | Reviews ([[domain\|bookworm]])                                                                                                                                 |
| **Owner Team**               | [[team\|catalog-experience-team]], [[team\|catalog-experience-team]]                                                                                           |
| **Hosting Service**          | [[service\|rating-api]]                                                                                                                                        |
| **LLM Provider & Model**     | Microsoft Foundry `model-router` (`2025-11-18`; `temperature: 0.4`, `maxOutputTokens: 1500`, `reasoningEffort: medium`)                                        |
| **Interaction Protocol**     | Remote A2A gRPC server (`rating_read` policy, Entra Agent Identity); in-process workflow invocation; MCP Streamable HTTP (outbound to [[service\|mcp-server]]) |
| **Memory / Context Scope**   | Stateless per-request with four-stage context compaction; summarized results cached by [[service\|rating-api]] in [[container\|rating-cache]]                  |
| **Fallback Behavior**        | Re-runs reduction up to configured iteration limits when confidence is low; fails closed on PII or write-policy violations                                     |

## Tools & Capabilities

<AgentTools />

| Capability / Tool    | Access Level | Target Resource                | Guardrail / Constraint                                                                   |
| :------------------- | :----------- | :----------------------------- | :--------------------------------------------------------------------------------------- |
| `GetCustomerReviews` | Read         | [[container\|rating-database]] | Read-only partition query; rate-limited to `30/minute` by governance policy              |
| `get_book`           | Read         | [[service\|mcp-server]]        | Always invoked before `GetCustomerReviews` to refresh catalog metadata and review counts |

## Data, Messaging & Handoff Boundaries

- **Data Stores:** Read-only consumer of [[container|rating-database]] via `GetCustomerReviews`. Generated summaries and quality signals are never persisted back to [[container|rating-database]] by the agent; [[service|rating-api]] caches [[query|SummarizeFeedback]] outputs in [[container|rating-cache]].
- **Messages:** Screens all inputs and outputs via [[service|presidio-api]] using [[command|AnalyzeText]] and [[command|AnonymizeText]]. Stored customer reviews in [[container|rating-database]] remain unmodified.
- **Delegation & Handoffs:** Receives remote A2A gRPC requests from [[service|chat-api]] (typically handed off by [[agent|book-agent]] after resolving a single book identifier). It never calls [[service|chat-api]] or peer conversational agents.

<Steps>
<Step title="Partition and map review batches">

For [[query|SummarizeFeedback]], the summarization workflow partitions customer reviews into bounded batches and fans out parallel structured summarization calls to Rating Agent.

</Step>
<Step title="Reduce and assess quality signals">

A reduce stage merges batch outputs into unified sentiment, recurring themes, strengths, weaknesses, and confidence-scored quality signals, repeating reduction if confidence falls below threshold.

</Step>
<Step title="Apply deterministic tier classification">

Deterministic service logic evaluates review count, average rating, and dominant negative pattern flags to assign `No Data`, `Bad`, `Good`, or `Top Rated`—never delegated to the LLM.

</Step>
</Steps>

## Safety, Privacy & AI Governance

- **Fail-Closed PII Redaction:** [[service|presidio-api]] (`AnalyzeText` and `AnonymizeText`) screens model inputs and outputs alongside regex PII patterns (`email`, `phone`, `credit_card`), ensuring reviewer personal details are masked before reaching the model or caller.
- **Default-Deny & Write Protection:** High-priority governance rules (`block-write-operations`, `block-destructive-sql`) block all mutations and non-`SELECT` SQL patterns, while capping `GetCustomerReviews` at `30/minute`.
- **Dual Output Guardrails:** Combines shared prompt/response guardrails with Rating-specific output checks across both buffered and streaming responses.
- **Four-Stage Context Compaction:** Applies tool-result compaction (`> 20` messages), LLM summarization (`> 3,000` tokens), sliding window (`> 12` turns), and hard truncation (`> 24,000` tokens) to prevent context overflow on large review sets.

<Admonition type="warning">
	Rating Agent is explicitly instructed never to emit a final quality classification or equate
	review counts with sales volume. All write operations are denied by policy, and final quality
	tiers are always computed deterministically outside the model.
</Admonition>

<Accordion title="Deterministic Classification Rules & Fallback Behavior">

| Classification | Condition                                                                                              |
| :------------- | :----------------------------------------------------------------------------------------------------- |
| **No Data**    | Fewer than `3` reviews, or non-finite / out-of-range average rating                                    |
| **Bad**        | Average rating `< 3.5`, or a dominant negative pattern is detected                                     |
| **Top Rated**  | (`averageRating >= 4.5` and `reviewCount >= 50`) or (`averageRating >= 4.0` and `reviewCount >= 200`)  |
| **Good**       | Remaining books with `reviewCount >= 3` and `averageRating >= 3.5` without a dominant negative pattern |

- **Low-Confidence Refinement:** If parsed assessment confidence is below threshold, the workflow loops reduction up to its configured maximum iterations before deterministic classification.

</Accordion>

## Architecture Visualizations

<NodeGraph />

**Review Intelligence Invocation and Data Boundary**

<MermaidFileLoader file="mermaids/rating-agent-diagram-1.mmd" />

## Related Documentation

- [[service|rating-api]] — Hosting service, [[query|SummarizeFeedback]] endpoint, and A2A gRPC server.
- [[container|rating-database]] — Authoritative Azure Cosmos DB feedback store.
- [[container|rating-cache]] — Caching layer for [[query|SummarizeFeedback]] results.
- [[agent|book-agent]] — Conversational specialist that resolves a book ID before A2A handoff.
- [[flow|agent-orchestration]] — Conversational workflow routing and A2A handoff flow.
- [[doc|arc42/08-crosscutting-concepts/005-ai-governance]] — Cross-cutting AI governance rules and standards.
- [[adr|adr-bw-014-composable-ai-governance]] — Shared governance composition across AI paths.
- [[adr|adr-004-read-only-governed-feedback-summarization]] — Read-only governed feedback summarization decision.
