Metadata-Version: 2.4
Name: llm-free-pool
Version: 0.1.1
Summary: One chat call across several free LLM tiers, with Retry-After cooldowns, used-up-plan benches and hedging
License: MIT OR Apache-2.0
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE-APACHE
License-File: LICENSE-MIT
Requires-Dist: httpx>=0.24
Dynamic: license-file

# llm-free-pool

**One chat call across several free LLM tiers, built to survive their quirks.**

Free tiers are generous but flaky: rate limits with different rules, models that disappear, trial credits that run
out, replies that come back empty, and models that sit silent for 30 seconds. This small library hides that behind
one call. It grew out of a personal AI that has run on free models around the clock on one home PC, where these
quirks happen hundreds of times a day.

```python
import free_llm_pool

answer = free_llm_pool.chat("Explain recursion in one sentence.")
print(answer.text, answer.provider, answer.model)
```

## Install

```
pip install llm-free-pool
```

(The import name is `free_llm_pool`; the PyPI name differs because `freellmpool` was already taken.)

Then set the API keys for whichever free tiers you have (any subset works). The keys stay in your own environment;
the library sends each one only in its own provider's `Authorization` header and never writes it to disk.

| Provider | Environment variable |
|---|---|
| Groq | `GROQ_API_KEY` |
| Cerebras | `CEREBRAS_API_KEY` |
| Mistral | `MISTRAL_API_KEY` |
| NVIDIA (build.nvidia.com) | `NVIDIA_API_KEY` |
| Cohere | `COHERE_API_KEY` |

## What it does on every call

- **Strongest model first.** Each provider's live model list is ranked by "prefer" words (e.g. `gpt-oss-120b`,
  `70b`); speech, embedding and vision models are skipped. Weaker models are only a last resort.
- **Rate limits that respect the server.** A 429 rests that model for exactly the server's `Retry-After`
  (seconds or an HTTP date), or for a "try again in 7m12s" hint in the body.
- **Used-up free plans.** A 402, or a 401/403/429 that says the trial, credits or subscription ran out, benches the
  whole provider for 1 day, then 2, 4 ... up to 30 days while it keeps happening. One real answer resets it.
  A 401/403 about one gated model ("the model X requires a subscription") rests only that model.
- **Empty replies, dead models, unreachable hosts.** An empty answer or a connection failure/timeout rests the
  model 5 minutes; a 404 rests it a day. Retry-After is capped at one day.
- **Too-large prompts.** A "context too long" reply moves to the next model without punishing this one.
- **Hedging.** If the first model hasn't sent a single token after 6 seconds, the next one starts too, and whichever
  speaks first answers (the other is cancelled). At most `max_parallel` (default 2) requests are in flight, since
  each one spends a free-tier request.
- **Shared state.** Pass `state_path=` to keep cooldowns and benches in a JSON file, so restarts and several
  processes don't hammer a limited model (each save merges what other processes wrote).

```python
import asyncio
from free_llm_pool import Pool

pool = Pool(state_path="pool_state.json", hedge_after=6.0)
answer = asyncio.run(pool.chat([{"role": "user", "content": "Hi"}], max_tokens=200))
```

`free_llm_pool.chat()` uses `asyncio.run`, so inside Jupyter or another running event loop use
`await Pool().chat(...)` instead.

Add your own OpenAI-compatible endpoint by passing `providers=`:

```python
Pool(providers={"mylocal": {"base": "http://localhost:8080/v1", "prefer": ["qwen"]}})
```

## Please use free tiers fairly

Free tiers exist at each provider's discretion. Follow their terms of use, use one account per provider, and don't
use this to get around limits they set. It is built to *respect* rate limits, not dodge them. Some free tiers (e.g.
NVIDIA's trial) may log prompts, so don't send anything private you wouldn't want stored.

## Tests

`python -m unittest discover -s tests -t .` runs everything against local fake servers (no network, no keys):
rankings, Retry-After cooldowns, doubling plan benches, empty replies, too-large prompts and hedging.

## License

Licensed under either of [MIT](LICENSE-MIT) or [Apache License 2.0](LICENSE-APACHE), at your option. Unless you say
otherwise, any contribution you submit is dual-licensed the same way.

Also from the same experiment: [evolve-guard](https://github.com/onlychaos420/Evolve-guardian), static checks that
stop an LLM self-improvement loop from merging fake improvements.
