Skip to content

Commit faab88f

Browse files
committed
docs: update capturing of token counts when using the decorator
1 parent bbdb29b commit faab88f

File tree

5 files changed

+101
-8
lines changed

5 files changed

+101
-8
lines changed

components-mdx/get-started-python-decorator-any-llm.mdx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@ def anthropic_completion(**kwargs):
1717
metadata=kwargs_clone
1818
)
1919

20+
response = anthopic_client.messages.create(**kwargs)
21+
22+
# See docs for more details on token counts and usd cost in Langfuse
23+
# https://langfuse.com/docs/model-usage-and-cost
24+
langfuse_context.update_current_observation(
25+
usage={
26+
"input": response.usage.input_tokens,
27+
"output": response.usage.output_tokens
28+
}
29+
)
30+
2031
# return result
21-
return anthopic_client.messages.create(**kwargs).content[0].text
32+
return response.content[0].text
2233

2334
@observe()
2435
def main():

cookbook/python_decorators.ipynb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,19 @@
236236
" metadata=kwargs_clone\n",
237237
" )\n",
238238
" \n",
239+
" response = anthopic_client.messages.create(**kwargs)\n",
240+
"\n",
241+
" # See docs for more details on token counts and usd cost in Langfuse\n",
242+
" # https://langfuse.com/docs/model-usage-and-cost\n",
243+
" langfuse_context.update_current_observation(\n",
244+
" usage={\n",
245+
" \"input\": response.usage.input_tokens,\n",
246+
" \"output\": response.usage.output_tokens\n",
247+
" }\n",
248+
" )\n",
249+
"\n",
239250
" # return result\n",
240-
" return anthopic_client.messages.create(**kwargs).content[0].text\n",
251+
" return response.content[0].text\n",
241252
"\n",
242253
"@observe()\n",
243254
"def main():\n",
@@ -256,7 +267,7 @@
256267
"cell_type": "markdown",
257268
"metadata": {},
258269
"source": [
259-
"> **Example trace**: https://cloud.langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/ece9079d-e12c-4c0e-9dc3-8805d0bbe8ec?observation=723c04ff-cdca-4716-8143-e691129be315"
270+
"> **Example trace**: https://cloud.langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/66d06dd7-eeec-40c1-9b11-aac0e9c4f2fe?observation=d48a45f8-593c-4013-8a8a-23665b94aeda"
260271
]
261272
},
262273
{

pages/docs/model-usage-and-cost.mdx

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,56 @@ If available in the LLM response, ingesting usage and/or cost is the most accura
3838

3939
Many of the Langfuse integrations automatically capture usage and cost data from the LLM response. If this does not work as expected, please create an [issue](/issue) on GitHub.
4040

41-
<Tabs items={["Python", "JS"]}>
41+
<Tabs items={["Python (Decorator)", "Python (low-level SDK)", "JS"]}>
42+
<Tab>
43+
44+
```python
45+
@observe(as_type="generation")
46+
def anthropic_completion(**kwargs):
47+
# optional, extract some fields from kwargs
48+
kwargs_clone = kwargs.copy()
49+
input = kwargs_clone.pop('messages', None)
50+
model = kwargs_clone.pop('model', None)
51+
langfuse_context.update_current_observation(
52+
input=input,
53+
model=model,
54+
metadata=kwargs_clone
55+
)
56+
57+
response = anthopic_client.messages.create(**kwargs)
58+
59+
langfuse_context.update_current_observation(
60+
usage={
61+
"input": response.usage.input_tokens,
62+
"output": response.usage.output_tokens,
63+
# "total": int, # if not set, it is derived from input + output
64+
"unit": "TOKENS", # any of: "TOKENS", "CHARACTERS", "MILLISECONDS", "SECONDS", "IMAGES"
65+
66+
# Optionally, also ingest usd cost. Alternatively, you can infer it via a model definition in Langfuse.
67+
# Here we assume the input and output cost are 1 USD each.
68+
"input_cost": 1,
69+
"output_cost": 1,
70+
# "total_cost": float, # if not set, it is derived from input_cost + output_cost
71+
}
72+
)
73+
74+
# return result
75+
return response.content[0].text
76+
77+
@observe()
78+
def main():
79+
return anthropic_completion(
80+
model="claude-3-opus-20240229",
81+
max_tokens=1024,
82+
messages=[
83+
{"role": "user", "content": "Hello, Claude"}
84+
]
85+
)
86+
87+
main()
88+
```
89+
90+
</Tab>
4291
<Tab>
4392

4493
```python

pages/docs/sdk/python/example.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,19 @@ def anthropic_completion(**kwargs):
156156
metadata=kwargs_clone
157157
)
158158

159+
response = anthopic_client.messages.create(**kwargs)
160+
161+
# See docs for more details on token counts and usd cost in Langfuse
162+
# https://langfuse.com/docs/model-usage-and-cost
163+
langfuse_context.update_current_observation(
164+
usage={
165+
"input": response.usage.input_tokens,
166+
"output": response.usage.output_tokens
167+
}
168+
)
169+
159170
# return result
160-
return anthopic_client.messages.create(**kwargs).content[0].text
171+
return response.content[0].text
161172

162173
@observe()
163174
def main():
@@ -172,7 +183,7 @@ def main():
172183
main()
173184
```
174185

175-
> **Example trace**: https://cloud.langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/ece9079d-e12c-4c0e-9dc3-8805d0bbe8ec?observation=723c04ff-cdca-4716-8143-e691129be315
186+
> **Example trace**: https://cloud.langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/66d06dd7-eeec-40c1-9b11-aac0e9c4f2fe?observation=d48a45f8-593c-4013-8a8a-23665b94aeda
176187
177188
## Customize input/output
178189

pages/guides/cookbook/python_decorators.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,19 @@ def anthropic_completion(**kwargs):
156156
metadata=kwargs_clone
157157
)
158158

159+
response = anthopic_client.messages.create(**kwargs)
160+
161+
# See docs for more details on token counts and usd cost in Langfuse
162+
# https://langfuse.com/docs/model-usage-and-cost
163+
langfuse_context.update_current_observation(
164+
usage={
165+
"input": response.usage.input_tokens,
166+
"output": response.usage.output_tokens
167+
}
168+
)
169+
159170
# return result
160-
return anthopic_client.messages.create(**kwargs).content[0].text
171+
return response.content[0].text
161172

162173
@observe()
163174
def main():
@@ -172,7 +183,7 @@ def main():
172183
main()
173184
```
174185

175-
> **Example trace**: https://cloud.langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/ece9079d-e12c-4c0e-9dc3-8805d0bbe8ec?observation=723c04ff-cdca-4716-8143-e691129be315
186+
> **Example trace**: https://cloud.langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/66d06dd7-eeec-40c1-9b11-aac0e9c4f2fe?observation=d48a45f8-593c-4013-8a8a-23665b94aeda
176187
177188
## Customize input/output
178189

0 commit comments

Comments
 (0)