# Usage


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

``` python
import litellm, importlib, httpx
from lisette.core import Chat, AsyncChat, patch_litellm
from cachy import enable_cachy, disable_cachy
from fastcore.test import *
```

## Lisette Usage Logger

``` python
importlib.reload(litellm); # to re-run the notebook without kernel restart
```

``` python
# litellm._turn_on_debug()
```

``` python
patch_litellm()
```

The precomputed response cost provided is available in
`kwargs['response_cost']` according to the [litellm
docs](https://docs.litellm.ai/docs/observability/custom_callback#whats-available-in-kwargs):

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

### LisetteUsageLogger

``` python
def LisetteUsageLogger(
    db_path
):
```

*Args:* turn_off_message_logging: bool - if True, the message logging
will be turned off. Message and response will be redacted from
StandardLoggingPayload. message_logging: bool - deprecated param, use
`turn_off_message_logging` instead

## Cost Utils

``` python
class PrefixDict(dict):
    def __getitem__(self, key):
        if key in self.keys(): return super().__getitem__(key)
        for k in self.keys(): 
            if key.startswith(k): return super().__getitem__(k)
        raise KeyError(key)
```

``` python
model_prices = PrefixDict({'claude-sonnet-4-5':
    dict(input_prc = 3/1e6, cache_write_prc = 3.75/1e6, cache_read_prc = 0.3/1e6, output_prc = 15/1e6, web_search_prc = 10/1e3)
})
```

Simplified cost utils to demonstrate total cost calculation (use
`Usage.response_cost` in prod):

``` python
@patch(as_prop=True)
def inp_cost(self:UsageStats):         return model_prices[self.model]['input_prc'] * (self.prompt_tokens - self.cache_read_tokens)
@patch(as_prop=True)
def cache_write_cost(self:UsageStats): return model_prices[self.model]['cache_write_prc'] * self.cache_creation_tokens
@patch(as_prop=True)
def cache_read_cost(self:UsageStats):  return model_prices[self.model]['cache_read_prc'] * self.cache_read_tokens
@patch(as_prop=True)
def out_cost(self:UsageStats):         return model_prices[self.model]['output_prc'] * self.completion_tokens
@patch(as_prop=True)
def web_cost(self:UsageStats):         return model_prices[self.model]['web_search_prc'] * (getattr(self, 'web_search_requests', 0) or 0)
@patch(as_prop=True)
def cost(self:UsageStats):             return self.inp_cost + self.cache_write_cost + self.cache_read_cost + self.out_cost + self.web_cost
```

A mapping of model pricing is also available in litellm, which is used
to calculate the `response_cost`

``` python
model_pricing = dict2obj(httpx.get(litellm.model_cost_map_url).json())
```

``` python
# model_pricing['claude-sonnet-4-5']
```

``` python
# model_pricing['gemini-3-pro-preview']
```

## Examples

``` python
from tempfile import NamedTemporaryFile
tf =NamedTemporaryFile(suffix='.db')
```

``` python
@patch
def user_id_fn(self:LisetteUsageLogger): return 'user-123'
tf=NamedTemporaryFile(suffix='.db')
logger = LisetteUsageLogger(tf.name)
litellm.callbacks = [logger]
```

``` python
slc = ','.join('id model user_id prompt_tokens completion_tokens total_tokens cached_tokens cache_creation_tokens cache_read_tokens web_search_requests response_cost'.split())
```

``` python
# litellm.set_verbose = True
```

A simple example:

``` python
chat = Chat('claude-sonnet-4-5-20250929')
r = chat("What is 2+2?")
```

``` python
time.sleep(0.1)
```

``` python
u = logger.usage(select=slc)[-1]; u
```

Our calculated cost matches litellm’s `response_cost`. In some cases it
might be better to use the custom calculation as we’ll see in the
remaining of this notebook:

``` python
test_eq(u.cost, u.response_cost)
```

Now, let’s test with streaming:

``` python
chat = Chat('claude-sonnet-4-5')
res = chat("Count from 1 to 5", stream=True)
for o in res: pass
```

``` python
time.sleep(0.1)
u = logger.usage(select=slc)[-1]; u
```

``` python
test_eq(u.cost, u.response_cost)
```

Streaming logged successfully. Let’s also verify async chat calls are
logged properly.

``` python
chat_async = AsyncChat('claude-sonnet-4-5-20250929')
await chat_async("What is 3+3?")
```

``` python
time.sleep(0.1)
u = logger.usage(select=slc)[-1]; u
```

``` python
test_eq(u.cost, u.response_cost)
```

Finally, let’s test async streaming to ensure all API patterns are
covered.

``` python
res = await chat_async("Count from 10 to 15", stream=True)
async for o in res: pass
print(o)
```

``` python
time.sleep(0.1)
u = logger.usage(select=slc)[-1]; u
```

``` python
test_eq(u.cost, u.response_cost)
```

### Search

Now let’s run a prompt with web search:

``` python
flash = 'gemini/gemini-3-flash-preview'
chat = Chat(flash)
chat("What is the weather like in NYC? Search web.", search="m")
```

``` python
time.sleep(0.1)
u = logger.usage(select=slc)[-1]; u
```

``` python
assert u.web_search_requests
```

``` python
chat = Chat('claude-sonnet-4-5-20250929')
r = chat("What is the weather like in NYC? Search web.", search="m")
```

``` python
time.sleep(0.1)
u = logger.usage(select=slc)[-1]; u
```

``` python
assert u.web_search_requests
```

<div>

> **Important**
>
> Litellm’s `response_cost` doesn’t take web search request cost into
> account!

</div>

Now, this is a case where using the custom calculations is better as it
will also include the web search request cost:

``` python
test_eq(u.cost, u.response_cost + u.web_search_requests * model_prices[u.model]['web_search_prc'])
```

### Search with streaming

Web search with streaming:

<div>

> **Important**
>
> Gemini web search requests are part of `prompt_tokens_details` which
> is only included with `stream_options={"include_usage": True}` when
> `stream=True`.
>
> There is currently a bug with gemini web search request counts,
> [Issue](https://github.com/BerriAI/litellm/issues/17919) and
> [PR](https://github.com/BerriAI/litellm/pull/17921). Waiting for
> litellm 1.80.11 pypi release.

</div>

``` python
chat = Chat(flash)
res = chat("What is the weather like in NYC? Search web.", search="m", stream=True, stream_options={"include_usage": True})
for o in res: pass
# print(o)
```

``` python
time.sleep(0.1)
u = logger.usage(select=slc)[-1]; u
```

<div>

> **Important**
>
> Anthropic web search requests are available in `usage.server_tool_use`

</div>

``` python
chat = Chat('claude-sonnet-4-5')
res = chat("What is the weather like in NYC now? Search web", search="m", stream=True, stream_options={"include_usage": True})
for o in res: pass
# print(o)
```

``` python
time.sleep(0.1)
u = logger.usage(select=slc)[-1]; u
```

``` python
test_eq(u.cost, u.response_cost + u.web_search_requests * model_prices[u.model]['web_search_prc'])
```

``` python
test_eq(len(logger.usage()), 8)
```

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

### total_cost

``` python
def total_cost(
    sc:float=0.01
):
```

*Call self as a function.*

``` python
L(logger.usage()).attrgot('response_cost').sum()
```

``` python
disable_cachy()
```

A simple Gemini example (requires min tokens and running twice to see
`cached_tokens`):

``` python
# #| notest
# chat = Chat('gemini/gemini-2.5-flash')
# chat("What is 2+2?"* 500)
# time.sleep(5)
# chat("What is 2+2?"* 500)
```

``` python
# #| notest
# time.sleep(0.1) # wait for callback db write
# u = logger.usage(select=slc)[-1];u
```

``` python
# #| notest
# test_eq(len(logger.usage()), 10)
# test_eq(logger.usage()[-1].cached_tokens > 3000, True)
```

``` python
tf.close()
```
