# Model Fallback

## Objective

Keep AI features working when the primary model provider fails.

A bounded retry path that reruns a failed AI task on a compatible alternative model and discloses the substitution.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Classify failures before reacting. Provider outages, capacity rejections, rate limiting, and timeouts are worth retrying elsewhere; a refusal, an invalid request, or an authentication error will fail identically on any destination and must surface immediately.
2. Honour rate limiting with backoff before moving on. Retrying instantly against a provider that asked you to wait deepens the outage and can extend the penalty period.
3. Allow fallback only to models that can honour the same output shape and the same tool behaviour as the original. A substitute that returns a different structure turns a provider outage into a data problem inside the app.
4. Make every step of the task idempotent, keyed to the original request, so a retry that follows a partially completed run does not repeat an external action. A tool call that sent a message or charged a card must not fire twice because the generation step failed after it.
5. Cap the attempts, the elapsed time, and the total spend for a single task, and fail cleanly when the cap is reached. Do not retry indefinitely down a list of models; an unbounded fallback chain converts a short outage into a large bill and a very slow error.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Only retryable provider-side and capacity errors should trigger a fallback. Retrying a refusal, a malformed request, or an expired credential wastes the budget and delays the real error reaching the user.
- The alternative model must produce the same output shape and support the same tools, and its result must be validated against the expected structure before it is accepted.
- If the failure happened after a tool call that had an external effect, the retry must not repeat that effect. Key each external action to the original request and check before acting.
- When a materially different model produced the result, say so on the output itself. A user comparing two results needs to know one of them came from a substitute.
- Attempts, elapsed time, and spend must all be bounded, and the task must fail with a clear message when a bound is hit rather than continuing down the list.
- Every fallback must be recorded with the trigger, the original destination, the substitute, and the outcome, so a pattern of substitutions is visible before it shows up as a quality complaint.
- When every option fails, the feature must degrade to something usable — the last saved result, a manual path, or a clearly stated unavailable state — rather than an unexplained error.
- A queued or background task must not restart its fallback chain from the beginning on every worker retry, or one outage produces many multiples of the intended attempts.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Fallback triggers only on retryable provider, capacity, rate-limit, and timeout errors.
- [ ] Rate-limited responses are backed off before any alternative is attempted.
- [ ] Substitute models are restricted to those matching the required output shape and tool behaviour, and their output is validated before acceptance.
- [ ] External actions taken before a failure are not repeated when the task is retried.
- [ ] Results produced by a substitute model are labelled as such on the output.
- [ ] Attempts, elapsed time, and spend per task are capped, and exhausting them produces a clear failure and a usable degraded state.
- [ ] Every fallback is recorded with its trigger, destinations, and outcome.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
