From b9be68cc45bd4678c883104b6f0b6142f2bc8a86 Mon Sep 17 00:00:00 2001 From: mubashir1osmani Date: Sat, 6 Sep 2025 22:24:49 -0400 Subject: [PATCH] spend tracking details --- docs/my-website/docs/proxy/enterprise.md | 301 +---------------------- 1 file changed, 3 insertions(+), 298 deletions(-) diff --git a/docs/my-website/docs/proxy/enterprise.md b/docs/my-website/docs/proxy/enterprise.md index 7d50aedb42..42677264ff 100644 --- a/docs/my-website/docs/proxy/enterprise.md +++ b/docs/my-website/docs/proxy/enterprise.md @@ -357,308 +357,13 @@ curl -X GET "http://0.0.0.0:4000/spend/tags" \ "total_spend": 0.000224 } ] - ``` +:::tip +For comprehensive spend tracking features including budgets, alerts, and detailed analytics, check out [Spend Tracking](https://docs.litellm.ai/docs/proxy/cost_tracking). -### Tracking Spend with custom metadata +::: -Requirements: - -- Virtual Keys & a database should be set up, see [virtual keys](https://docs.litellm.ai/docs/proxy/virtual_keys) - -#### Usage - /chat/completions requests with special spend logs metadata - - - - - -```bash -curl -L -X POST 'http://0.0.0.0:4000/key/generate' \ --H 'Authorization: Bearer sk-1234' \ --H 'Content-Type: application/json' \ --d '{ - "metadata": { - "spend_logs_metadata": { - "hello": "world" - } - } -} - -' -``` - - - - -```bash -curl -L -X POST 'http://0.0.0.0:4000/team/new' \ --H 'Authorization: Bearer sk-1234' \ --H 'Content-Type: application/json' \ --d '{ - "metadata": { - "spend_logs_metadata": { - "hello": "world" - } - } -} - -' -``` - - - - - -Set `extra_body={"metadata": { }}` to `metadata` you want to pass - -```python -import openai -client = openai.OpenAI( - api_key="anything", - base_url="http://0.0.0.0:4000" -) - -# request sent to model set on litellm proxy, `litellm --model` -response = client.chat.completions.create( - model="gpt-3.5-turbo", - messages = [ - { - "role": "user", - "content": "this is a test request, write a short poem" - } - ], - extra_body={ - "metadata": { - "spend_logs_metadata": { - "hello": "world" - } - } - } -) - -print(response) -``` - -**Using Headers:** - -```python -import openai -client = openai.OpenAI( - api_key="sk-1234", - base_url="http://0.0.0.0:4000" -) - -# Pass spend logs metadata via headers -response = client.chat.completions.create( - model="gpt-3.5-turbo", - messages = [ - { - "role": "user", - "content": "this is a test request, write a short poem" - } - ], - extra_headers={ - "x-litellm-spend-logs-metadata": '{"user_id": "12345", "project_id": "proj_abc", "request_type": "chat_completion"}' - } -) - -print(response) -``` - - - - - - -```js -const openai = require('openai'); - -async function runOpenAI() { - const client = new openai.OpenAI({ - apiKey: 'sk-1234', - baseURL: 'http://0.0.0.0:4000' - }); - - try { - const response = await client.chat.completions.create({ - model: 'gpt-3.5-turbo', - messages: [ - { - role: 'user', - content: "this is a test request, write a short poem" - }, - ], - metadata: { - spend_logs_metadata: { // 👈 Key Change - hello: "world" - } - } - }); - console.log(response); - } catch (error) { - console.log("got this exception from server"); - console.error(error); - } -} - -// Call the asynchronous function -runOpenAI(); -``` - -**Using Headers:** - -```js -const openai = require('openai'); - -async function runOpenAI() { - const client = new openai.OpenAI({ - apiKey: 'sk-1234', - baseURL: 'http://0.0.0.0:4000' - }); - - try { - const response = await client.chat.completions.create({ - model: 'gpt-3.5-turbo', - messages: [ - { - role: 'user', - content: "this is a test request, write a short poem" - }, - ] - }, { - headers: { - 'x-litellm-spend-logs-metadata': '{"user_id": "12345", "project_id": "proj_abc", "request_type": "chat_completion"}' - } - }); - console.log(response); - } catch (error) { - console.log("got this exception from server"); - console.error(error); - } -} - -// Call the asynchronous function -runOpenAI(); -``` - - - - - -Pass `metadata` as part of the request body - -```shell -curl --location 'http://0.0.0.0:4000/chat/completions' \ - --header 'Content-Type: application/json' \ - --data '{ - "model": "gpt-3.5-turbo", - "messages": [ - { - "role": "user", - "content": "what llm are you" - } - ], - "metadata": { - "spend_logs_metadata": { - "hello": "world" - } - } -}' -``` - - - - - -Pass `x-litellm-spend-logs-metadata` as a request header with JSON string - -```shell -curl --location 'http://0.0.0.0:4000/chat/completions' \ - --header 'Content-Type: application/json' \ - --header 'Authorization: Bearer sk-1234' \ - --header 'x-litellm-spend-logs-metadata: {"user_id": "12345", "project_id": "proj_abc", "request_type": "chat_completion"}' \ - --data '{ - "model": "gpt-3.5-turbo", - "messages": [ - { - "role": "user", - "content": "what llm are you" - } - ] -}' -``` - - - - -```python -from langchain.chat_models import ChatOpenAI -from langchain.prompts.chat import ( - ChatPromptTemplate, - HumanMessagePromptTemplate, - SystemMessagePromptTemplate, -) -from langchain.schema import HumanMessage, SystemMessage - -chat = ChatOpenAI( - openai_api_base="http://0.0.0.0:4000", - model = "gpt-3.5-turbo", - temperature=0.1, - extra_body={ - "metadata": { - "spend_logs_metadata": { - "hello": "world" - } - } - } -) - -messages = [ - SystemMessage( - content="You are a helpful assistant that im using to make a test request to." - ), - HumanMessage( - content="test from litellm. tell me why it's amazing in 1 sentence" - ), -] -response = chat(messages) - -print(response) -``` - - - - - -#### Viewing Spend w/ custom metadata - -#### `/spend/logs` Request Format - -```bash -curl -X GET "http://0.0.0.0:4000/spend/logs?request_id=