> For the complete documentation index, see [llms.txt](https://seekspeed.gitbook.io/seekspeed-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://seekspeed.gitbook.io/seekspeed-docs/optimization-engine/rules.md).

# Rule Catalog

The optimization engine is intentionally rule-based, not LLM-based. Every recommendation is deterministic, inspectable, and cheap to compute. The catalog lives in `src/lib/recommendations.ts`.

| Rule               | Triggers when…                                                 | Suggests                                                       |
| ------------------ | -------------------------------------------------------------- | -------------------------------------------------------------- |
| Slim system prompt | `systemTokens > 1200` and `p95Ttft > 800ms`                    | Auto-generate a trimmed variant by extracting stable rules.    |
| Model route down   | Task complexity score < threshold and `gpt-4`-class model used | Swap to `gpt-4o-mini` / `llama-3.1-8b` for the easy subset.    |
| Semantic cache     | High prompt similarity rate across iterations                  | Add an embedding-cache wrapper in front of the agent.          |
| Retry/backoff      | Error rate > 2% with timeouts                                  | Add exponential backoff with jitter; cap retries at 2.         |
| Token budget       | `tokens_out` consistently < `max_tokens × 0.4`                 | Lower `max_tokens` — frees scheduler slots on the server side. |
| Tool prune         | Tool used in < 20% of turns                                    | Move tool to a conditional planner step.                       |
| Streaming on       | Streaming disabled, TTFT not measurable                        | Turn on `stream: true` and recapture.                          |
| Concurrency cap    | p95 climbs > 2× between concurrency 1 and 4                    | Reduce client concurrency or add server replicas.              |

## Each rule defines an `apply()` function

```ts
type Recommendation = {
  id: string;
  title: string;
  rationale: string;
  expectedImpact: string;
  difficulty: "low" | "medium" | "high";
  confidence: number;       // 0..1
  apply: (agent: AgentProfile) => AgentProfile;   // pure, returns variant
};
```

This is what makes the apply → measure → accept loop possible. A recommendation that cannot synthesise a variant is downgraded to "advisory only" and never offers an Apply button.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://seekspeed.gitbook.io/seekspeed-docs/optimization-engine/rules.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
