Use observability findings to build a reproducible test before choosing a fix. Preserve a redacted trace, record the versions and data used, identify the failing component, and describe what the system should have done. Then compare the proposed change with the current system on that case and a broader evaluation set. LangSmith explains how production monitoring and offline evaluation support this process.
For RAG, check document availability, retrieval, ranking, context selection, and whether the answer follows its sources. For agents, also check tool selection, arguments, permissions, retries, and stopping conditions. A wrong answer can originate in any of these steps, so changing the prompt should follow the diagnosis.
Release gradually once the tests pass. Monitor the original failure type alongside task success, latency, and cost, and keep a rollback ready. Include similar but previously unseen cases in testing so the change solves more than the one incident that prompted it.
The trace-to-fix loop
The practical sequence is:
Finding in production → safe incident record → reproduction → failure label and owning layer → versioned regression case → candidate intervention → offline evidence → shadow or canary → release → continued monitoring
Repeat these checks as new failures appear. A later trace can show that the original diagnosis was incomplete, that a fix moved the failure elsewhere, or that a data or model change invalidated an earlier conclusion. NIST recommends iterative, documented test, evaluation, validation, and verification, and cautions that lab testing may not transfer cleanly to the real deployment context. NIST AI 600-1, pp. 51-52.
1. Capture a useful trace without creating a data leak
Capture enough structure to reconstruct the decision, not an unrestricted transcript of every user interaction. Give every span a trace ID and correlate it with immutable identifiers for the application build, prompt or workflow version, model and decoding settings, tool schema version, policy bundle, index and corpus snapshot, embedding and reranker version, feature flags, region, and a pseudonymous session or tenant ID. Record timing, error types, token use, retry count, and outcome labels as structured fields. Common semantic names make traces portable across libraries and back ends; OpenTelemetry’s semantic conventions exist specifically to give telemetry a shared meaning across producers and consumers. OpenTelemetry semantic conventions.
For a RAG run, retain document IDs, document versions, filter expression or filter hash, candidate ranks and scores, reranker inputs and outputs, selected chunk IDs, context order, truncation decisions, and citations shown to the user. For an agent run, retain the requested capability, tool name and schema version, normalized arguments, validation result, policy or authorization decision ID, executor identity, external response class, state transition, and side-effect idempotency key. Store encrypted pointers or hashes for raw content where feasible instead of copying content into general-purpose logs. The standardized GenAI attributes warn that system instructions, tool-call arguments, and tool results can contain sensitive information, which is a reason to make raw-content capture explicitly opt-in and access-controlled. OpenTelemetry GenAI attribute registry.
Redact before telemetry leaves the request boundary. Apply allowlists for fields that may leave the service, detect and replace secrets and direct identifiers, separate mapping keys from the trace store, enforce role-based access, set a documented retention period, and audit trace reads. Do not send raw production traces to an external evaluator or annotator without an approved data path. Collect full detail for errors, policy denials, user-reported failures, and an intentionally sampled set of normal executions. For high-volume traffic, combine a small random baseline with stratified sampling by tenant, language, workflow, model version, outcome, and latency or cost bucket. Sampling only failures makes diagnosis faster but cannot tell whether a change has damaged healthy traffic; sampling only successes hides long-tail harms.
2. Freeze and reproduce the incident
A trace can be suggestive while still being unreproducible. External corpora change, a vector index is rebuilt, a model is updated, a tool returns live data, and generation is often nondeterministic. Create a protected incident record that includes the redacted input, the parent trace and relevant spans, exact configuration IDs, and the observed outcome. Preserve a replayable snapshot or content-addressed references for retrieved documents and tool fixtures. If a real tool call could write, pay, publish, delete, or expose data, replay it against a sandbox, recorded response, or dry-run executor. Never replay a production side effect merely to see whether it happens again.
Reproduction should determine whether the current or historical system can produce the observed failure under stated conditions. Begin with the same versions and fixture data. Then vary one factor at a time, such as the retrieval filter, context budget, temperature, tool response, or authorization claim. Record confidence honestly. A failure that occurs once in a stochastic model may be a low-probability defect rather than a deterministic bug. Run multiple seeds or repeated trials, report the rate and uncertainty, and avoid claiming a single replay proves causation.
3. Label the failure before selecting a fix
Use a compact taxonomy that maps the observed symptom to a responsible layer. One trace can receive more than one label, but choose one primary failure to drive the first regression case and assign an owner. This prevents teams from changing the answer prompt when the real defect was an index filter or an authorization boundary.
| Primary layer | What failed | Evidence to inspect | Typical intervention |
|---|---|---|---|
| Retrieval | The relevant source was absent from candidates | Corpus version, chunk ID, access filter, query rewrite, recall at candidate cutoff | Repair ingestion, chunking, metadata, query transformation, embedding model, or retrieval filter |
| Ranking | The source was retrieved but placed below the cutoff | Candidate list, scores, reranker features, selected rank | Tune or replace reranking, diversify candidates, adjust cutoff with holdout evidence |
| Context construction | Good sources existed but were excluded, truncated, duplicated, or misleadingly ordered | Chosen chunks, token budget, deduplication, formatting, source recency | Change packing, budget allocation, source selection, citation placement, or conflict handling |
| Generation | The answer contradicted or failed to use adequate context | Context-to-answer support, citation alignment, decoding settings, refusal and format checks | Improve instruction, output schema, model choice, grounding check, or abstention behavior |
| Tool use | The agent chose a wrong tool, malformed arguments, or misread the result | Tool schema, normalized arguments, validation, response, retries | Narrow tools, strengthen schema validation, add preconditions, improve result parsing, or alter planning |
| Authorization | An action or data access was allowed outside the user’s scope | Acting principal, policy decision, scopes, downstream audit record | Enforce server-side policy, least privilege, user-scoped credentials, approval gate |
| Orchestration | The workflow looped, retried badly, lost state, exceeded budget, or handed off incorrectly | State transitions, retries, stop condition, queue and timeout events | Add bounded retries, idempotency, state validation, budget limit, fallback, or human escalation |
The table is a diagnostic starting point, not proof. For example, a poor answer with no useful source in context may be retrieval or context construction, while a perfect tool choice that returns another customer’s record is authorization. Treat policy enforcement as a deterministic downstream responsibility, not a behavior the model should remember from its prompt. OWASP's current agent guidance emphasizes transparency, traceability, and enforceable runtime controls. OWASP Agent Control Standard.
From one trace to a durable regression case
Create a dataset example only after the incident has been reviewed and redacted. It should contain the input and relevant state, immutable fixture references, intended outcome, prohibited outcome, expected evidence or tool effects, taxonomy labels, severity, provenance, and links to the original trace. Do not encode an arbitrary wording as the sole golden answer when many answers are valid. For RAG, expected evidence can be a required current document and a claim-support rubric. For agents, it can be an allowed capability set, required authorization result, expected state transition, and expected final effect.
Version the dataset as carefully as code. Tag each example with the application, corpus, index, embedding, model, tool-schema, and policy versions it was created against. Preserve the original incident example even if documentation later changes, then add a new example for the changed policy or corpus. Versioned datasets let a continuous-integration job state exactly which evidence it tested rather than silently changing the test underneath a release. This practice aligns with guidance to separate validation and test splits and to tag dataset versions for important milestones and CI use. LangSmith evaluation concepts.
Do not let a few anecdotes become the optimization target. Keep the incident in a development or diagnostic split, create nearby validation cases, and reserve a time-separated or otherwise unseen holdout set. Cluster near-duplicate user prompts before splitting so paraphrases of the same incident do not appear in both development and test. Balance slices that matter to the product, such as language, tenant type, document freshness, complexity, tool, authorization state, and failure class. Include negative cases such as irrelevant documents, obsolete sources, ambiguous requests, unavailable tools, hostile tool output, missing privileges, and timeout conditions. The documented dataset workflow recommends manually curated, high-quality examples, production traces, and category or ML-style splits, rather than relying on a single source. LangSmith evaluation concepts.
Counterfactual checks
Counterfactual tests change one meaningful condition while holding the intended rule constant. They are particularly good at catching a cosmetic fix that memorizes a trace.
- For retrieval, swap the query’s region, product, date, or entitlement and verify that the relevant source changes appropriately. Add a similarly worded stale document and verify that it does not win solely because it is lexically familiar.
- For context construction, remove the decisive source and expect abstention or a request for clarification instead of a confident answer. Add an irrelevant long chunk and verify that it does not crowd out the relevant evidence.
- For generation, keep the same sources but invert a factual constraint in the user question. Check that the answer follows the source and exposes uncertainty or conflict rather than repeating a template.
- For a tool, keep the natural-language request but change the caller’s identity, scope, account state, or approval status. The executor should deny or simulate the action according to policy even if the model still asks for it.
These tests exercise the claimed mechanism. If a retrieval change only passes the original wording but fails when the user’s region changes, it has not fixed access-aware retrieval. If a prompt change makes an agent say it needs permission while the downstream tool still accepts a generic administrator credential, it has not fixed authorization.
Choose the intervention that matches the layer
Prefer the smallest change that directly corrects the classified mechanism and preserves security boundaries. A prompt edit may be appropriate for a generation instruction failure, but it is a weak response to a missing document or absent authorization check. Make one candidate change at a time when possible. If a coupled change is necessary, document each component and compare against the current production baseline, not merely an earlier prototype.
For retrieval, first validate source availability, ACL filters, document freshness, chunk boundaries, and query normalization before replacing an embedding model. For ranking, test whether the needed result is in the candidate set and whether a reranker or cutoff loses it. For context construction, examine token allocation, duplicate chunks, recency, conflict resolution, and citation mapping. For generation, add structured outputs, explicit unsupported-answer behavior, deterministic checks for citations or schemas, or a better-suited model only after confirming the context was sufficient.
For tool-use failures, narrow the tool contract and validate arguments before execution. Separate planning from execution so the executor receives a typed request plus authenticated identity, not free-form model text. For authorization, do not accept a model’s claim that an action is permitted. The service that owns the resource must evaluate the authenticated principal, resource, operation, and policy. For orchestration, make retries bounded, use idempotency keys for effects, validate state transitions, set time and cost budgets, and define a safe fallback or escalation path. The risk is material. OWASP's current guidance treats excessive agency as a central LLM application risk and provides a separate control standard for runtime enforcement. OWASP GenAI LLM Top 10 2026.
Evaluate the change before it reaches users
Use a layered evaluation, because no single score answers both “does it work?” and “is it safe to deploy?” Run deterministic tests first: ACL and policy decisions, tool argument schemas, idempotency, output JSON validity, document version constraints, and hard budget limits. A deterministic failure blocks release. Then run component and end-to-end evaluations over the fixed dataset with the baseline and candidate configurations. Record environment, corpus, model, prompt, tool, and evaluator versions for each experiment.
For RAG, report candidate recall of required sources, ranking quality, context sufficiency, claim support or faithfulness, answer correctness, citation accuracy, abstention quality, latency, and cost. A good answer score cannot compensate for retrieval that silently leaks documents across tenants. For agents, report valid tool selection, argument validity, policy compliance, completion of the intended state transition, forbidden side-effect rate, loop or retry rate, latency, and cost. Measure slices, not only an aggregate. A change that helps English FAQ queries but harms a low-volume language or a sensitive workflow needs an explicit tradeoff decision.
Use humans for nuanced factuality, usefulness, and trajectory quality, with a written rubric and blinded review where possible. Have overlapping reviewers label a sample, resolve disagreements, and periodically recheck agreement after the rubric changes. An LLM judge can scale rubric-based scoring, but it is an instrument that needs calibration, not ground truth. Test it against held-out human labels, report disagreements and false positives or negatives, version its prompt and model, and route uncertain cases to human review. Framework documentation likewise separates deterministic trajectory matching, useful when expected tool calls are known, from LLM-as-judge assessment of qualitative trajectory quality. LangChain Agent Evals.
Set release gates before looking at the candidate result. For example, require no new critical authorization violations, no drop beyond an agreed tolerance on protected slices, improvement or non-inferiority on the incident class and holdout set, and acceptable latency and cost. This makes the decision auditable and avoids inventing a favorable threshold after an experiment succeeds.
Worked RAG example
Setup. This hypothetical support assistant answers, “Can an EU subscriber cancel after downloading a digital item?” A negative-feedback trace has an answer based on a generic policy. Its retrieval span shows that the current EU policy document exists in the corpus but was excluded by a default region=US filter. The generation span received only the generic source, so calling this a hallucination would misclassify the failure.
Action. The team creates a redacted incident fixture containing the normalized request, an EU entitlement claim, the corpus snapshot, filter version, candidate IDs, selected context, and response. It adds a regression case requiring the current EU policy as eligible evidence and requiring the assistant to request clarification when the region is absent. The intervention maps the trusted account-region claim into the retrieval filter and rejects an unknown region rather than defaulting to US. Offline evaluation compares the baseline and change across EU, US, unknown-region, stale-document, and multilingual cases. Counterfactuals change only the region or add a closely worded obsolete document. The team evaluates source recall, selected-source correctness, supported answer quality, abstention behavior, and latency on a heldout set.
If the candidate improves EU source recall but worsens unknown-region answers, the fix is not ready. The service can shadow the new retrieval path and compare source selections without changing user responses. A small canary may then serve answers with user feedback and a rollback flag. Post-release monitoring should track the original wrong-region-filter label, current-source selection, unsupported-answer rate, missing-region clarification rate, and slice metrics by region. In this case, repair access-aware retrieval and keep a regression test that checks it.
Worked agent tool-call example
Setup. This hypothetical account agent receives “Cancel my subscription.” A trace shows it selects the cancel_subscription tool with a valid account ID, but the executor uses a shared service credential and performs the cancellation even when the caller is not the account owner. The model’s wording and tool selection may be perfectly plausible. The primary label is authorization, with a secondary tool-use label only if the tool choice or arguments were also wrong.
Action. Freeze a safe fixture with caller identity class, target account ownership, entitlement state, approval state, tool request, and recorded result. Add deterministic tests for owner, non-owner, support delegate, expired approval, and malformed account ID. Replace the executor contract so it receives an authenticated acting principal, calls the downstream authorization service, and uses a user-scoped or minimally privileged credential. Require explicit confirmation for cancellation and make the cancellation endpoint idempotent. The agent may propose a call, but cannot override a server-side denial. Evaluate allowed and forbidden trajectories, policy outcomes, duplicate-call behavior, and final account state. An exact tool-call sequence can be tested when policy requires one; otherwise test the permitted capability set and effect so harmless alternate plans are not penalized.
Takeaway. Run the new workflow in dry-run or sandbox mode first. A shadow mode for a destructive action must simulate the effect and record what policy would have decided, never cancel real subscriptions in parallel. Canary only a low-risk cohort with confirmation, rate limits, immediate disable capability, and audit trails. Monitor denial rate by reason, attempted cross-account access, confirmation abandonment, tool errors, duplicate-effect prevention, and unexpected cancellations. This corrects the security boundary even if a future model call is confused or manipulated.
Release and monitoring plan
Before release, write a short change record: incident link, primary label, owner, hypothesis, exact intervention, dataset and evaluator versions, baseline and candidate results by slice, known tradeoffs, rollout cohort, alerts, rollback mechanism, and review date. Make the feature flag capable of disabling the new behavior without an emergency code change. For a model, prompt, corpus, or tool update, re-run the relevant regression and holdout suites because those changes can alter behavior after the original code fix.
Shadow traffic is appropriate when it does not create user-visible or external side effects. Compare candidate and baseline retrieval sets, rankings, generated responses, or simulated tool decisions offline. A canary is appropriate when live feedback and dependencies matter: expose the candidate to a small, predeclared cohort, enforce spending and action caps, and watch both quality and safety. For write-capable agents, a read-only simulation, staged approval, or reversible operation is usually safer than a conventional shadow call.
After release, keep the incident label as an online monitor and add rate, severity, and slice dashboards. Alert on recurrence, sharp baseline deviation, policy violations, user-negative feedback, evidence drift, tool error changes, unexpected cost or latency, and unreviewed evaluator disagreement. Feed representative new traces back into the dataset only after redaction and review. Production monitoring generates candidates for the offline suite; offline evaluation validates a change before deployment; the next production traces confirm whether the improvement holds. That division is the useful boundary between observing a problem and demonstrating a fix. LangSmith’s evaluation lifecycle describes the same offline and online feedback loop.
Limits and judgment calls
Observability cannot reveal every root cause. A trace may omit an upstream data mutation, user context, provider failure, or hidden model behavior. Replay cannot fully reproduce a live, nondeterministic model or rapidly changing web corpus. Evaluator scores can be biased, and a favorable offline result does not prove live-user benefit. Use confidence levels, human review for high-impact cases, carefully sampled monitoring, and controlled rollout to manage these gaps rather than concealing them.
Do not automatically treat every low score as a defect. Some requests are ambiguous, unsafe, outside the supported corpus, or unauthorized. A correct result may be a clarification, refusal, escalation, or policy denial. Define those acceptable outcomes in the regression case and make them visible in metrics, otherwise teams will tune the system toward confident but unsafe completion.
Evidence
Sources used for this answer.
Question signals show what people need. Primary documentation supports the answer. Both remain visible.
- 01Trace-to-Fix: how are you actually improving RAG/agents after observability flags issues?Hugging Face Forums · question signal · checked 4 Sept 2026
- 02LangSmith explains how production monitoring and offline evaluation support this processdocs.langchain.com · primary evidence · checked 4 Sept 2026
- 03OpenTelemetry GenAI attribute registryopentelemetry.io · primary evidence · checked 4 Sept 2026
- 04NIST AI 600-1, pp. 51-52nvlpubs.nist.gov · primary evidence · checked 4 Sept 2026
- 05OpenTelemetry semantic conventionsopentelemetry.io · primary evidence · checked 4 Sept 2026
- 06OWASP Agent Control Standardgenai.owasp.org · primary evidence · checked 4 Sept 2026
- 07OWASP GenAI LLM Top 10 2026genai.owasp.org · primary evidence · checked 4 Sept 2026
- 08LangChain Agent Evalsdocs.langchain.com · implementation guidance · checked 4 Sept 2026