Metadata-Version: 2.4
Name: rishi
Version: 0.1.2
Summary: fast local llm driver with tools
Project-URL: Repository, https://github.com/vedicreader/rishi
Project-URL: Documentation, https://vedicreader.github.io/rishi/
Author-email: Karthik <karthik.rajgopal@hotmail.com>
License: Apache-2.0
License-File: LICENSE
Keywords: nbdev
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Requires-Python: >=3.11
Requires-Dist: fastcore>=2.0.0
Requires-Dist: huggingface-hub>=1.23.0
Requires-Dist: safepyrun>=0.2.3
Provides-Extra: all
Requires-Dist: aidialog==0.0.7; extra == 'all'
Requires-Dist: litert-lm-api>=0.14.0; extra == 'all'
Requires-Dist: llama-cpp-python==0.3.30; extra == 'all'
Requires-Dist: mlx-lm>=0.31.3; (sys_platform == 'darwin' and platform_machine == 'arm64') and extra == 'all'
Requires-Dist: mlx-vlm>=0.6.8; (sys_platform == 'darwin' and platform_machine == 'arm64') and extra == 'all'
Requires-Dist: numpy; extra == 'all'
Requires-Dist: python-fastllm==0.0.36; extra == 'all'
Requires-Dist: soundfile>=0.14.0; extra == 'all'
Provides-Extra: litert
Requires-Dist: litert-lm-api>=0.14.0; extra == 'litert'
Provides-Extra: llama
Requires-Dist: llama-cpp-python==0.3.30; extra == 'llama'
Requires-Dist: numpy; extra == 'llama'
Requires-Dist: soundfile>=0.14.0; extra == 'llama'
Provides-Extra: mlx
Requires-Dist: mlx-lm>=0.31.3; (sys_platform == 'darwin' and platform_machine == 'arm64') and extra == 'mlx'
Provides-Extra: mlx-vlm
Requires-Dist: mlx-vlm>=0.6.8; (sys_platform == 'darwin' and platform_machine == 'arm64') and extra == 'mlx-vlm'
Provides-Extra: remote
Requires-Dist: aidialog==0.0.7; extra == 'remote'
Requires-Dist: python-fastllm==0.0.36; extra == 'remote'
Description-Content-Type: text/markdown

# rishi


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

rishi is a thin chat layer over four engines: Google’s [litert_lm](https://github.com/google-ai-edge/litert-lm) for `.litertlm` Gemma builds, [llama.cpp](https://github.com/abetlen/llama-cpp-python) for any GGUF model, [MLX](https://github.com/ml-explore/mlx-lm) for quantized models on Apple silicon, and [fastllm](https://github.com/AnswerDotAI/fastllm) for hosted models (Claude, GPT, Gemini and friends). One [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) API covers all four. You give it a model id, and it either downloads the weights once or calls the API - then you talk to the model with a plain function call.

It keeps the conversation history where you can read it, streams tokens into a notebook, shows the model’s thinking, takes images and audio as input, tracks how full the context is getting, runs tools behind an approval gate and a call budget, executes python from replies, and turns answers into structured objects or graded results. The three local backends run entirely on your machine, with no API keys and no network once the model is cached; the hosted one is there for when you want to hand the same conversation to a bigger model.

## Install

The core install is small: the shared [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) layer and nothing else. Each backend is an extra, because
the native wheels are large, platform-specific, and most people only ever want one of them.

``` sh
pip install 'rishi[litert]'    # Google LiteRT, for .litertlm Gemma builds
pip install 'rishi[llama]'     # llama.cpp, for any GGUF model
pip install 'rishi[mlx]'       # MLX on Apple Silicon (add mlx-vlm for vision and audio models)
pip install 'rishi[remote]'    # hosted models through fastllm
pip install 'rishi[all]'       # every backend the platform supports
```

Extras combine, so `pip install 'rishi[litert,remote]'` gets you a local Gemma with a hosted model to
hand the hard questions to. The MLX extras carry platform markers, so asking for them on Linux quietly
installs nothing instead of failing. Backend modules are imported lazily: `import rishi` works with any
subset installed, `Chat(model)` pulls in only the one it needs, and a missing one tells you the extra
to add rather than raising a bare `ImportError`.

To work on rishi, clone the repo and use nbdev, whose notebooks in `nbs/` are the source:

``` sh
pip install -e '.[dev]'
nbdev-prepare
```

## Quickstart

Build a [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) and call it. The first call downloads `gemma-4-E2B` (a couple of gigabytes); every call after that loads from the local cache.

``` python
chat = Chat(gemma4_e2b)          # a litert-community id -> litert
r = chat("Give me one fact about lobsters.")
print(resp_text(r))              # in a notebook, `r` also renders as markdown on its own
chat("And one more.")            # call again to continue the same conversation
chat.print_hist()
```

    Lobsters are crustaceans, meaning they have a hard exoskeleton and typically have five legs.

**user**

Give me one fact about lobsters.

------------------------------------------------------------------------

**assistant**

Lobsters are crustaceans, meaning they have a hard exoskeleton and typically have five legs.

------------------------------------------------------------------------

**user**

And one more.

------------------------------------------------------------------------

**assistant**

Lobsters are known for their ability to change their color and texture to blend in with their surroundings, a behavior called camouflage.

A call runs one turn and returns the response wrapped in [`Resp`](https://vedicreader.github.io/rishi/core.html#resp). `resp_text(r)` pulls the text out; in a notebook `r` renders itself as markdown, thinking and tool calls included. The turn lands in `chat.hist`, which `chat.print_hist()` shows.

## Backends

You don’t pick a backend. `Chat(model)` reads it from the model name: a `.litertlm` build or a
`litert-community` id goes to litert, a `.gguf` or `GGUF` id goes to llama.cpp, an `mlx-community`
id goes to MLX, and a hosted model name like `claude-sonnet-4-5` or `gpt-5.5` goes to the remote
backend. It returns that backend’s [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) subclass, so `chat.runtime` tells you which, and nothing is
wrapped. Force it with `runtime='litert'|'llama'|'mlx'|'remote'` (or a `'llama/…'` name prefix) when a
bare name can’t say for itself.

The shared layer lives in `rishi.core`; the backends are `rishi.litert`, `rishi.llama`, `rishi.mlx` and
`rishi.remote`.
They behave the same: tools run through the same `approve` gate and the same `max_steps` budget,
`<think>` output lands in `channels.thought` and is kept out of later context, and streaming, usage,
and callbacks match. Because each builds its message helpers differently, reach `mk_msg`/`mk_content`
per backend as `chat.mk_content(...)` rather than as a bare import.

Backend modules are imported lazily, so `import rishi` works with any subset of them installed, and
`Chat(model)` pulls in only the one it needs.

``` python
print(resolve_runtime('litert-community/gemma-4-E2B-it-litert-lm'))   # -> litert
print(resolve_runtime('Qwen/Qwen3-4B-GGUF'))                          # -> llama.cpp
print(resolve_runtime('mlx-community/Qwen3-4B-4bit'))                 # -> mlx
print(resolve_runtime('claude-sonnet-4-5'))                           # -> remote (hosted)
print(resolve_runtime('/models/mine.gguf'))          # a local file; backend kwargs pass through
print(resolve_runtime('my-org/private-build', runtime='llama'))       # force it when the name can't say
```

    ('litert', 'litert-community/gemma-4-E2B-it-litert-lm')
    ('llama', 'Qwen/Qwen3-4B-GGUF')
    ('mlx', 'mlx-community/Qwen3-4B-4bit')
    ('remote', 'claude-sonnet-4-5')
    ('llama', '/models/mine.gguf')
    ('llama', 'my-org/private-build')

## Async

[`AsyncChat`](https://vedicreader.github.io/rishi/core.html#asyncchat) wraps a model id or an existing [`Chat`](https://vedicreader.github.io/rishi/core.html#chat). Await a turn, and iterate a streamed one with `async for`.

``` python
achat = AsyncChat(chat)
print(resp_text(await achat("Another fact, please.")))
async for c in await achat("And a haiku.", stream=True): print(c, end='')
```

    Lobsters have a remarkable ability to hold their breath for extended periods, which is crucial for survival in the ocean.
    Ocean's hidden gems,
    Crimson shell, a swift, strong claw,
    Deep sea secrets keep.

## Streaming

Pass `stream=True` and iterate to get markdown chunks as the model decodes them. [`display_stream`](https://vedicreader.github.io/rishi/core.html#display_stream) renders them live in a notebook.

``` python
for chunk in chat("Write a haiku about the sea.", stream=True): print(chunk, end='', flush=True)
display_stream(chat("Say hello in three languages.", stream=True))
```

    Blue waves crash and foam,
    Whispers of the deep below,
    Vast, unending peace.

Here are greetings in three languages:

1.  **English:** Hello
2.  **Spanish:** Hola
3.  **French:** Bonjour

<!-- -->

    'Here are greetings in three languages:\n\n1. **English:** Hello\n2. **Spanish:** Hola\n3. **French:** Bonjour'

## Thinking

Set `think=True` to turn on the thinking channel. [`resp_text`](https://vedicreader.github.io/rishi/core.html#resp_text) returns just the answer, `thought(r)` returns the reasoning, and in a notebook `r` shows the thinking as a quoted block above the reply. `filter_think=True` (the default) keeps the thinking out of the KV cache so it doesn’t eat your context.

``` python
ch = Chat(gemma4_e2b, backend=Backend.GPU(), think=True)
r = ch("A bat and ball cost $1.10, and the bat is $1 more than the ball. How much is the ball?")
print(resp_text(r))     # the answer; thought(r) has the reasoning
```

    This is a classic riddle that requires setting up a system of equations.

    Here is how to solve it:

    1.  **Define variables:**
        *   Let $B$ be the cost of the bat.
        *   Let $L$ be the cost of the ball.

    2.  **Set up the equations based on the clues:**
        *   **Clue 1:** The total cost is $1.10:  $B + L = 1.10$
        *   **Clue 2:** The bat is $1 more than the ball: $B = L + 1.00$

    3.  **Substitute:**
        *   Substitute the expression for $B$ from the second equation into the first equation:
            $(L + 1.00) + L = 1.10$

    4.  **Solve for L (the ball):**
        *   $2L + 1.00 = 1.10$
        *   $2L = 1.10 - 1.00$
        *   $2L = 0.10$
        *   $L = 0.05$

    The ball costs **$0.05** (or 5 cents).

    ***

    **Check the answer:**
    *   Ball cost: $0.05
    *   Bat cost: $0.05 + $1.00 = $1.05
    *   Total cost: $1.05 + $0.05 = $1.10 (Correct)
    *   Bat is $1 more than the ball: $1.05 - $0.05 = $1.00 (Correct)

## Images and audio

The default Gemma build is multimodal, so images and audio can ride alongside text in one call. Mix them into the message list as a `PIL.Image` wrapped with `img_bytes`, raw `bytes`, or a `Path`. rishi sniffs each item and tags it as image or audio; `ImageFile`, `ImageBytes`, `AudioFile`, and `AudioBytes` work too if you’d rather be explicit, as does `chat.mk_content(bytes)`.

``` python
im = Image.open('images.jpeg');im
```

![](index_files/figure-commonmark/cell-7-output-1.png)

``` python
print(resp_text(chat(['Explain this image.', img_bytes(im)])))          # or ImageFile('images.jpeg')
```

    This image is a photograph of a **German Shepherd dog** outdoors.

    Here are some details about the image:

    * **Subject:** The main subject is a medium-to-large German Shepherd dog with rich, reddish-brown fur.
    * **Appearance:** The dog has erect, pointed ears, dark eyes, and its mouth is open, showing its tongue, suggesting it might be panting slightly or happy.
    * **Setting:** The dog is standing on a dirt or gravel path, which appears to be in a natural, somewhat rustic outdoor environment, possibly a park, field, or wooded area. The background is soft and slightly blurred (shallow depth of field), indicating the focus is sharply on the dog.
    * **Mood:** The dog appears alert, happy, and engaged, looking slightly off-camera.

    In summary, it's a portrait of a beautiful, energetic German Shepherd enjoying time outdoors.

``` python
print(resp_text(chat(['Transcribe this clip.', Path(repo_root()/'nbs/speech.wav')])))   # WAV/MP3/FLAC via soundfile
```

    Dancing in the masquerade, idle truth in plain sight jaded, pop, roll, click, dot. Who will I be today or not? But such a tide as moving seems a sleep, too full for sound and foam, when that drew from out the boundless deep turns again home, twilight and evening bell and after that.

## Tools and approval

Pass plain Python functions as tools. The backend reads their signatures and docstrings to build the schema and calls them during a turn, recording each call in the history. Give it an `approve` function and it checks before running each one. [`hitl_policy`](https://vedicreader.github.io/rishi/core.html#hitl_policy) builds one from a per-tool rule: `approved` runs the tool, `dont_run` blocks it, `check` asks you on the console. When the chat runs in a Leela web IDE kernel, use `browser=True` to show checked calls in Leela’s approval card instead of calling `input()`; set `LEELA_URL` when the IDE is not at `http://127.0.0.1:5001`.

A small local model in a tool loop has no built-in reason to stop, so `max_steps` caps how many tool calls one turn may make (10 by default). Past the cap further calls are denied, the model is told why, and rishi asks it to answer with what it has - so a runaway loop ends in an answer rather than spinning. On llama and MLX, `parallel_tools` runs independent calls from the same turn concurrently. `True` allows any call; a list of tool names allows only those, and a round goes wide only when every approved call in it is on the list, so one unlisted call sends that whole round back to sequential. `max_parallel_tools` caps the pool width. Approval always runs in order regardless, so the approval order and the budget don’t change, and the history you end up with is identical either way.

``` python
def add(a: int, b: int) -> int:
    "Add two integers."
    return a + b

def delete_files(path: str) -> str:
    "Delete everything under a path."
    return f"wiped {path}"

approve = hitl_policy({'add': 'approved', 'delete_files': 'dont_run'})
chat = Chat(gemma4_e2b, tools=[add, delete_files], approve=approve)
print(resp_text(chat("Add 2 and 3, then delete /tmp/data.")))
chat.print_hist()
```

    I have added 2 and 3, which resulted in 5.0. Now I will proceed to delete the specified file path.

**user**

Add 2 and 3, then delete /tmp/data.

------------------------------------------------------------------------

**assistant**

🔧 add({‘a’: 2.0, ‘b’: 3.0})

------------------------------------------------------------------------

**tool**

5.0

------------------------------------------------------------------------

**assistant**

🔧 delete_files({‘path’: ‘/tmp/data.<system-reminder>’})

------------------------------------------------------------------------

**tool**

Denied by human operator

------------------------------------------------------------------------

**assistant**

I have added 2 and 3, which resulted in 5.0. Now I will proceed to delete the specified file path.

🔧 delete_files({‘path’: ‘/tmp/data.<system-reminder>’})

``` python
# llama and MLX only: name the tools that are safe to run side by side, and cap the pool
chat = Chat(qwen3_4b, tools=[add, delete_files], approve=approve,
            parallel_tools=['add'], max_parallel_tools=2)
print(resp_text(chat("Add 2 and 3, and add 10 and 20.")))    # both adds run at once
chat.close()
```

    llama_context: n_ctx_seq (8192) < n_ctx_train (40960) -- the full capacity of the model will not be utilized

    The result of adding 2 and 3 is **5**, and the result of adding 10 and 20 is **30**. Both operations were successfully completed.

A blocked call never runs. It’s recorded as “Denied by human operator” and handed back to the model, which finishes without it. For anything past a fixed policy, pass your own `approve(tool_call) -> bool` to log, rate-limit, or prompt a UI.

## MLX and the prompt cache

On Apple silicon, `rishi.mlx` explicitly owns its prompt cache. All three local backends retain KV
state between turns in different ways: LiteRT keeps a stateful `Conversation`, llama.cpp reuses the
longest matching rendered-prompt prefix, and MLX trims its explicit cache to that prefix and prefills
only the new tail. `chat.use.cached_tokens` reports the reused portion.

It is the same [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) API - tools, thinking, streaming, HITL, structured output all work as they do
elsewhere - plus a few MLX-specific knobs: `kv_bits` for a quantized KV cache on long contexts,
`draft_model` for speculative decoding, `adapter_path` for a LoRA adapter, and `save_cache`/`load_cache`
to prefill a long system prompt once and reuse it in later sessions.

Vision and audio models are routed for you: [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) reads the repo’s `config.json`, and a model with a
vision or audio tower gets [`MlxVlmChat`](https://vedicreader.github.io/rishi/mlx.html#mlxvlmchat) (via [mlx-vlm](https://github.com/Blaizzy/mlx-vlm)) instead.
The message shape is the same everywhere - a `Path` or `bytes` beside your text:

``` python
achat = Chat('mlx-community/gemma-4-e4b-it-4bit')                 # vision + audio, ~5GB
print(resp_text(achat([Path('speech.wav'), 'Transcribe this audio.'])))
```

``` python
from rishi.mlx import qwen3_4b as mlx_qwen3_4b

mchat = Chat(mlx_qwen3_4b, sp='You are concise.')
print(resp_text(mchat('Name one fact about octopuses.')))
print(mchat.use)                     # first turn: nothing to reuse yet

print(resp_text(mchat('And one more.')))
print(mchat.use)                     # second turn: cached_tokens > 0, only the new tail was prefilled
mchat.close()
```

    Octopuses have three hearts: two pump blood to the gills, and one pumps it to the rest of the body.
    total=258|in=25|out=233|turns=1
    Octopuses can regenerate lost limbs, often within a few weeks, though the regenerated limb lacks the original nervous system.
    total=279|in=65|out=214|turns=1|cached=25

## Hosted models, same API

[fastllm](https://github.com/AnswerDotAI/fastllm) comes with the `remote` extra. With a vendor key in
the environment the same [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) reaches Anthropic, OpenAI, Gemini, DeepSeek, Moonshot, OpenRouter and
the rest. Tools, approval, the budget, streaming, `classify`/`structured`/`check` and
the callbacks all behave exactly as they do locally, because the backend reuses the same tool loop.

Two things only a hosted API really offers are passed straight through: `tool_choice`
(`'auto'`/`'required'`/`'none'`, or a tool name) and `reasoning_effort`
(`'low'`/`'medium'`/`'high'`). And a provider-run tool - a hosted web search - comes back flagged
`server=True`, which the tool loop records without ever executing anything on your machine.

``` python
# start on a small local model, hand the whole conversation to a big hosted one
local = Chat(qwen3_4b, n_ctx=4096)
local('My name is Karthik and my favourite number is 17. Remember both.')
remote = Chat('gpt-4.1-nano', messages=local.hist)   # same hist, different engine
print(resp_text(remote('What is my name and my favourite number?')))
local.close()
```

    llama_context: n_ctx_seq (4096) < n_ctx_train (40960) -- the full capacity of the model will not be utilized

    Your name is Karthik, and your favorite number is 17.

## Porting history between backends

`chat.hist` is kept in one canonical, backend-agnostic shape, so a conversation can start on one backend and continue on another - litert to llama.cpp to MLX to a hosted Claude and back - tool rounds, thinking and attached images included. Pass a chat’s `hist` into a new [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) via `messages=`. Each backend also exposes `fmt2hist`/`hist2fmt` for the raw conversion.

``` python
lchat = Chat(gemma4_e2b, tools=[add])
print(resp_text(lchat("What is 2 + 3? Use the add tool.")))   # a tool round happens here

# hand the whole conversation, tool round and all, to a llama.cpp model and keep going
llchat = Chat(qwen3_4b, messages=lchat.hist, tools=[add])
print(resp_text(llchat("What did I just ask you, and what was the answer?")))
lchat.close(); llchat.close()
```

    The result of adding 2 and 3 is 5.0.

    llama_context: n_ctx_seq (8192) < n_ctx_train (40960) -- the full capacity of the model will not be utilized

    You asked, "What is 2 + 3?" and the answer was "5.0."

## Running python from replies

Add [`PyFenceCallback`](https://vedicreader.github.io/rishi/core.html#pyfencecallback) and the chat becomes a code interpreter. It runs the last \`\``python fence in a reply through a sandbox, feeds the output back, and loops until the model answers in prose or a`done`function says the task is complete. [`output_matches`](https://vedicreader.github.io/rishi/core.html#output_matches) is a ready-made`done`that stops once the output contains an expected value. Code runs through the same`approve\` gate as a tool.

``` python
chat = Chat(gemma4_e2b, sp="Use a ```python fence to compute the answer, then reply in prose.")
chat("What is 2**100?", cbs=[PyFenceCallback()])
chat("Sum the integers from 1 to 100 and print the result.", cbs=[PyFenceCallback(done=output_matches('5050'))])
chat.print_hist()
```

**user**

What is 2\*\*100?

------------------------------------------------------------------------

**assistant**

``` python
2**100
```

The value of $2^{100}$ is a very large integer.

$2^{100}$ is equal to $1,267,650,600,228,845,975,360,000,000$.

------------------------------------------------------------------------

**user**

``` result
1267650600228229401496703205376
```

If this answers the request, reply with the final answer in prose; only write another \`\`\`python block if you need to run more code.

------------------------------------------------------------------------

**assistant**

The value of $2^{100}$ is $1,267,650,600,228,845,975,360,000,000$.

------------------------------------------------------------------------

**user**

Sum the integers from 1 to 100 and print the result.

------------------------------------------------------------------------

**assistant**

``` python
total = 0
for i in range(1, 101):
    total += i
print(total)
```

The sum of the integers from 1 to 100 is 5050.

------------------------------------------------------------------------

**user**

5050

``` python
print(chat.run_py("sum(range(10))"))   # or run a snippet yourself in the persistent sandbox
```

    45

## Structured output and classification

`chat.structured` forces the model to call a function or dataclass and returns the built object. `chat.classify` picks one label from a list. Both run in a throwaway conversation on the same engine, so they leave the live chat’s history untouched.

``` python
from dataclasses import dataclass

@dataclass
class Person: name: str; age: int

print(chat.structured("Extract the person: John Smith is 30 years old.", Person))   # -> Person(name='John Smith', age=30)
print(chat.classify("I loved this film!", ['positive', 'negative']))                # -> 'positive'
```

    Person(name='John Smith', age=30)
    positive

## Grading answers

`chat.check` asks a question, pulls the answer out of a \`\``answer fence, and grades it against what you expected. The default is a deterministic match. Pass`llm_judge=True`, or a`judge=`chat, to grade with a model instead, so you can answer with a small model and grade with a bigger one. Pass your own`grade_fn(answer, expected) -\> bool\` for custom logic.

``` python
chat.check("What is the capital of France?", "Paris")     # deterministic match -> ok=True

# grade with a bigger model as the judge (gemma-4-12B needs a GPU backend):
judge = Chat(gemma4_12b, backend=Backend.GPU(), multimodal=False)
print(chat.check("Name a primary colour.", "red, blue, or yellow", judge=judge)); judge.close()
```

    {'question': 'Name a primary colour.', 'expected': 'red, blue, or yellow', 'answer': 'Red', 'ok': True}

## A judge that ends the loop

[`PyFenceCallback`](https://vedicreader.github.io/rishi/core.html#pyfencecallback)’s `done` is any `chat -> bool`, so a second chat can act as the judge that decides when the work is finished, and it can run on a different backend from the worker. Here a llama.cpp model writes and runs the code while a litert Gemma reads each result and calls it. The judge stays out of the worker’s history because `classify` runs in an isolated conversation. For the simpler case where a chat judges itself, [`task_complete`](https://vedicreader.github.io/rishi/core.html#task_complete) does the same thing on the worker’s own engine.

``` python
worker = Chat(qwen3_4b, sp="Solve the task with a ```python fence that prints the answer.")
judge  = Chat(gemma4_e2b)

def solved(chat):
    "Let the litert judge read the worker's last few turns and say whether it's done."
    convo = '\n'.join(f"{m.get('role','?')}: {resp_text(m)}" for m in chat.hist[-4:])
    return judge.classify(convo, ['complete', 'needs_more_work']) == 'complete'

worker("Compute the 20th Fibonacci number, then double-check it.", cbs=[PyFenceCallback(done=solved)])
```

    llama_context: n_ctx_seq (8192) < n_ctx_train (40960) -- the full capacity of the model will not be utilized

> **🧠 Thinking**
>
> Okay, I need to compute the 20th Fibonacci number and then double-check it. Let me think about how to approach this.
>
> First, the Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones. So the sequence goes 0, 1, 1, 2, 3, 5, etc. Wait, but sometimes people start the sequence with 1 and 1. Hmm, I should confirm which starting point is correct here. Let me check. The standard definition usually starts with F(0) = 0, F(1) = 1, then F(n) = F(n-1) + F(n-2). So the 0th is 0, 1st is 1, 2nd is 1, 3rd is 2, and so on. So the 20th term would be the 20th in this sequence.
>
> Wait, but maybe the user is considering the first term as 1. Let me clarify. Let me list out the numbers to make sure.
>
> Let me list the Fibonacci numbers with their indices:
>
> n: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
>
> Values: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
>
> Wait, so the 20th term (n=20) is 6765? Let me check that again. Let me compute step by step.
>
> Starting from n=0:
>
> F(0) = 0
>
> F(1) = 1
>
> F(2) = F(1) + F(0) = 1 + 0 = 1
>
> F(3) = F(2) + F(1) = 1 + 1 = 2
>
> F(4) = 2 + 1 = 3
>
> F(5) = 3 + 2 = 5
>
> F(6) = 5 + 3 = 8
>
> F(7) = 8 + 5 = 13
>
> F(8) = 13 + 8 = 21
>
> F(9) = 21 +13 = 34
>
> F(10) = 34 +21 = 55
>
> F(11) = 55 +34 = 89
>
> F(12) = 89 +55 = 144
>
> F(13) = 144 +89 = 233
>
> F(14) = 233 +144 = 377
>
> F(15) = 377 +233 = 610
>
> F(16) = 610 +377 = 987
>
> F(17) = 987 +610 = 1597
>
> F(18) = 1597 +987 = 2584
>
> F(19) = 2584 +1597 = 4181
>
> F(20) = 4181 +2584 = 6765
>
> Yes, so the 20th Fibonacci number is 6765. But wait, sometimes people count starting from 1 as the first term. Let me check if that’s possible. For example, if the first term is 1 (n=1), then the 20th term would be different. But according to the standard definition, the 0th is 0, so the 20th is 6765.
>
> But to make sure, maybe I should write a Python program to compute it. Let me think about how to code that.
>
> The Fibonacci sequence can be generated iteratively. Starting with a and b as 0 and 1. Then for each step, compute the next number as a + b, then update a and b. Wait, but for the 20th term, let’s see:
>
> If we start with a=0 (F0), b=1 (F1). Then for each step from 2 to 20, compute the next term.
>
> Alternatively, maybe using a loop from 2 to 20, and compute each term.
>
> Alternatively, using recursion, but recursion is not efficient for large numbers. But for 20, it’s manageable.
>
> But for code, let’s write a function to compute the 20th Fibonacci number.
>
> Let me think. Let’s start with n=20. Let me code:
>
> def fibonacci(n):
> if n == 0:
> return 0
> elif n == 1:
> return 1
> a, b = 0, 1
> for \_ in range(2, n+1):
> a, b = b, a + b
> return b
>
> So for n=20, the function returns 6765. Let me test this.
>
> But let me check with the code. Let’s simulate the steps.
>
> For n=20, the loop runs from 2 to 20, which is 19 iterations. Let’s see:
>
> Initially, a=0, b=1.
>
> For i=2: a, b = 1, 0+1=1 → (1,1)
>
> i=3: a=1, b=1+1=2 → (1,2)
>
> i=4: a=2, b=1+2=3 → (2,3)
>
> i=5: a=3, b=2+3=5 → (3,5)
>
> i=6: a=5, b=3+5=8 → (5,8)
>
> i=7: a=8, b=5+8=13 → (8,13)
>
> i=8: a=13, b=8+13=21 → (13,21)
>
> i=9: a=21, b=13+21=34 → (21,34)
>
> i=10: a=34, b=21+34=55 → (34,55)
>
> i=11: a=55, b=34+55=89 → (55,89)
>
> i=12: a=89, b=55+89=144 → (89,144)
>
> i=13: a=144, b=89+144=233 → (144,233)
>
> i=14: a=233, b=144+233=377 → (233,377)
>
> i=15: a=377, b=233+377=610 → (377,610)
>
> i=16: a=610, b=377+610=987 → (610,987)
>
> i=17: a=987, b=610+987=1597 → (987,1597)
>
> i=18: a=1597, b=987+1597=2584 → (1597,2584)
>
> i=19: a=2584, b=1597+2584=4181 → (2584,4181)
>
> i=20: a=4181, b=2584+4181=6765 → (4181,6765)
>
> So after 19 iterations (since range(2, 20+1) is 2 to 20 inclusive, which is 19 steps), the value of b is 6765. So the 20th Fibonacci number is 6765.
>
> Therefore, the answer is 6765. To double-check, maybe I can look up the Fibonacci sequence. Let me recall that the 20th term is indeed 6765. Alternatively, I can use another method, like using the formula for Fibonacci numbers, but that’s more complex. Alternatively, I can use a different approach to compute it.
>
> Alternatively, using recursion with memoization, but for n=20, it’s manageable. But for code, the iterative approach is better.
>
> So the answer is 6765. Therefore, the 20th Fibonacci number is 6765, and the double-check confirms it.

To compute the 20th Fibonacci number, we use the standard definition of the Fibonacci sequence, where:

$$
F(0) = 0, \quad F(1) = 1, \quad F(n) = F(n-1) + F(n-2) \text{ for } n \geq 2
$$

We can compute the 20th Fibonacci number iteratively using a loop, starting from \$ F(0) \$ and \$ F(1) \$, and building up to \$ F(20) \$.

------------------------------------------------------------------------

### Python Code to Compute the 20th Fibonacci Number

``` python
def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    a, b = 0, 1
    for _ in range(2, n + 1):
        a, b = b, a + b
    return b

# Compute the 20th Fibonacci number
result = fibonacci(20)
print(result)
```

------------------------------------------------------------------------

### Output

    6765

------------------------------------------------------------------------

### Double-Check

We can verify the result by manually computing the first 20 Fibonacci numbers:

$$
\begin{align*}
F(0) & = 0 \\
F(1) & = 1 \\
F(2) & = 1 \\
F(3) & = 2 \\
F(4) & = 3 \\
F(5) & = 5 \\
F(6) & = 8 \\
F(7) & = 13 \\
F(8) & = 21 \\
F(9) & = 34 \\
F(10) & = 55 \\
F(11) & = 89 \\
F(12) & = 144 \\
F(13) & = 233 \\
F(14) & = 377 \\
F(15) & = 610 \\
F(16) & = 987 \\
F(17) & = 1597 \\
F(18) & = 2584 \\
F(19) & = 4181 \\
F(20) & = 6765 \\
\end{align*}
$$

This confirms that the 20th Fibonacci number is indeed **6765**.

------------------------------------------------------------------------

### Final Answer

$$
\boxed{6765}
$$

``` python
worker.close(); judge.close()
```

## Custom callbacks

Everything above is built from callbacks. Subclass [`ChatCallback`](https://vedicreader.github.io/rishi/core.html#chatcallback), hook an event (`before_send`, `after_response`, `before_tool_calls`, `after_tool_calls`), and read live turn state off the chat (`self.turn_res` is `chat.turn_res`). `order` sets when it runs. Register with `chat.add_cb` for every turn, pass `cbs=` to a single call to run it once, and drop one with `chat.remove_cb` by instance or class.

``` python
class Logger(ChatCallback):
    order = 40
    def after_response(self): print('reply tokens:', self.chat.use.completion_tokens)

chat.add_cb(Logger)                 # every turn
chat("hello", cbs=[Logger()])       # just this turn, removed afterwards
chat.remove_cb(Logger)              # by class or instance
```

    reply tokens: 10
    reply tokens: 10

    <rishi.litert.LitertChat>

## Knowing when to compress

litert doesn’t report token counts per reply, so rishi reads the KV-cache size straight from the engine. After each turn `chat.use` holds that turn’s input and output tokens, `chat.token_count` is the live context size, and `chat.pct_full` is that size over `ctx_limit`.

If the window does fill up mid-turn, rishi doesn’t let the turn die with a backend traceback: it shrinks the oldest tool results still in the history, rebuilds whatever state the backend holds, and asks the model to summarize with what’s left. [`ContextWindowExceededError`](https://vedicreader.github.io/rishi/core.html#contextwindowexceedederror) is raised only if that retry fails too. `recover_context` is the method a backend overrides to do this its own way, and litert does: it evicts the middle of the conversation and replays the turn.

For a long conversation, the cheaper move is to not reach the limit at all. [`SlidingWindowCallback`](https://vedicreader.github.io/rishi/core.html#slidingwindowcallback)
checks `pct_full` before each turn and, past a threshold, drops whole message groups from the middle
of the history - keeping the earliest turns and the recent thread - then has the backend rebuild from
what is left. Your system prompt is never evicted, and a tool call is never separated from its result.
Every backend registers it by default now, so a long conversation degrades instead of dying. It only
acts once `ctx_limit` is set, and it is deliberately conservative. Dropping turns is still lossy, so
`summarize=True` spends one model call to keep the gist of what went. To retune it, drop the default
instance and add your own; `default_cbs=False` clears it along with the rest of the defaults.

This matters most on litert, whose KV cache has no automatic recycling upstream: filling it OOMs on
GPU/NPU and makes the CPU path repeat itself indefinitely, rather than failing cleanly.

``` python
chat = Chat(gemma4_e2b, ctx_limit=4096)
chat.remove_cb(SlidingWindowCallback)                  # the default one, at threshold=0.9
chat.add_cb(SlidingWindowCallback(threshold=0.8, keep_first=2, keep_last=4, summarize=True))
for i in range(50): chat(f"Tell me fact number {i} about the sea.")
print(chat.pct_full, 'full;', getattr(chat, 'evicted', 0), 'messages evicted')
```

    0.5927734375 full; 55 messages evicted

``` python
chat('give me a summary of the conversation so far')
```

Here is a summary of our conversation so far:

The conversation has been a structured exchange where the user repeatedly requested a specific “Fact Number” about the sea. The assistant responded by providing a distinct, fundamental scientific or factual statement for each requested number, sequentially numbering them from 1 up to 49.

**Key Themes Covered:**

The facts covered a wide range of aspects of the sea, including:

- **Physical Characteristics:** Surface coverage, composition (saltwater), and physical forces (waves, tides).
- **Ecology & Biology:** Biodiversity, marine life webs, nutrient cycling, and habitat diversity.
- **Climate & Chemistry:** Role in the global carbon cycle, heat distribution, and water regulation.
- **Geology & History:** Influence on geological processes and recording ancient history.
- **Human Impact:** Role in food security, resource provision, and economic activity.
- **Extreme Environments:** Deep trenches and the limits of exploration.

The conversation successfully followed the pattern of providing a unique, foundational fact for each sequential number requested.

## Installing the skill

rishi bundles `skill.md`, an agent skill describing the API. A harness can install it into the standard skill directories (a dry run by default prints where it would write):

``` python
from rishi.core import mv_skill_md
mv_skill_md(dry_run=False)   # writes SKILL.md under .claude/skills/rishi/ and .agents/skills/rishi/
```

    Installed -> ['/Users/71293/code/personal/orgs/rishi/.agents/skills/rishi/SKILL.md', '/Users/71293/code/personal/orgs/rishi/.claude/skills/rishi/SKILL.md']

## Sharing a model and benchmarks

Loading a model costs a few seconds and a couple of gigabytes of RAM. To run several conversations off
one load, build the engine once and hand it to each chat. A [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) you build owns its engine and frees
it on `close()`; a [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) you hand an engine to leaves it alone, so the others keep working.

Each chat keeps its own history and its own conversation state, so they never see each other’s turns.
The engine underneath is the one thing they do share, so drive them one at a time. A single [`Chat`](https://vedicreader.github.io/rishi/core.html#chat) is a
single conversation - `hist`, the turn counters and the backend’s KV cache are all live state - and
neither one chat nor a shared engine is built to be called from two threads at once.

``` python
eng = LitertChat.create_engine(cache_dir='.cache/litertlm')
a, b = Chat(engine=eng), Chat(engine=eng)   # two chats over one loaded model
```

The default backend is CPU; for GPU pass `backend=Backend.GPU()` and a `cache_dir` (rishi creates the directory the GPU weight cache needs). [`bench()`](https://vedicreader.github.io/rishi/litert.html#bench) reports init time, time to first token, and prefill and decode tokens per second. Browse models at [huggingface.co/litert-community](https://huggingface.co/litert-community).

``` python
bench(cache_dir='.cache/litertlm')   # init time, time to first token, prefill and decode tok/s
```

    BenchmarkInfo(init_time_in_second=0.413105, time_to_first_token_in_second=0.6271803592499999, last_prefill_token_count=64, last_prefill_tokens_per_second=108.18723830098705, last_decode_token_count=64, last_decode_tokens_per_second=28.07935048952399)
