# llm\_service.rs — Multi-Provider Inference & Routing

#### 🌐 Core Trait

```rust
trait LLMProvider {
    fn generate(&self, prompt: &str, stream: bool) -> Result<String>;
    fn name(&self) -> &'static str;
}
```

Each provider implements this trait with their own:

* HTTP client
* Token limits
* Streaming options

***

#### 🧭 Provider Selection Logic

Oxyde includes a router that picks a provider based on:

| Factor           | Notes                                          |
| ---------------- | ---------------------------------------------- |
| Prompt length    | Short prompts → low-latency models (Groq)      |
| Emotion state    | Aggressive tone → model with higher creativity |
| API availability | Fall back if 429 errors occur                  |
| Config weights   | You can hard-prefer OpenAI, etc.               |

***

#### 🔁 Routing Example

```rust
let model = router.select_provider(&agent.emotion, &prompt);
let result = model.generate(&prompt, stream = false)?;
```

***

#### 🔒 Rate Limiting & Failover

If a provider returns:

* HTTP 429 → retry with exponential backoff
* HTTP 5xx → fallback to next preferred model

You can also implement caching or queueing middleware here.

***

#### 🔊 Streaming Support

If `stream = true`, providers return output token-by-token:

```rust
"Y... You shouldn't have come here..."
```

This is handled via `futures::Stream` in async Rust.

***

#### 🧩 Adding a New Provider

Just implement the `LLMProvider` trait:

```rust
pub struct MyCustomLLM { /* ... */ }

impl LLMProvider for MyCustomLLM {
    fn generate(&self, prompt: &str, stream: bool) -> Result<String> {
        // call your API here
    }
    fn name(&self) -> &'static str { "MyLLM" }
}
```

Then add to the router map in `llm_service.rs`.
