Skip to content

Commit 0006c10

Browse files
authored
Logfire docs (#106)
1 parent 5068a10 commit 0006c10

File tree

12 files changed

+85
-27
lines changed

12 files changed

+85
-27
lines changed

docs/.hooks/main.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,33 @@ def on_page_markdown(markdown: str, page: Page, config: Config, files: Files) ->
1818

1919

2020
def replace_uv_python_run(markdown: str) -> str:
21-
return re.sub(r'```bash\n(.*?)(python/uv[\- ]run|pip/uv[\- ]add)(.+?)\n```', sub_run, markdown)
21+
return re.sub(r'```bash\n(.*?)(python/uv[\- ]run|pip/uv[\- ]add|py-cli)(.+?)\n```', sub_run, markdown)
2222

2323

2424
def sub_run(m: re.Match[str]) -> str:
2525
prefix = m.group(1)
2626
command = m.group(2)
27-
install = 'pip' in command
27+
if 'pip' in command:
28+
pip_base = 'pip install'
29+
uv_base = 'uv add'
30+
elif command == 'py-cli':
31+
pip_base = ''
32+
uv_base = 'uv run'
33+
else:
34+
pip_base = 'python'
35+
uv_base = 'uv run'
2836
suffix = m.group(3)
2937
return f"""\
3038
=== "pip"
3139
3240
```bash
33-
{prefix}{'pip install' if install else 'python'}{suffix}
41+
{prefix}{pip_base}{suffix}
3442
```
3543
3644
=== "uv"
3745
3846
```bash
39-
{prefix}uv {'add' if install else 'run'}{suffix}
47+
{prefix}{uv_base}{suffix}
4048
```"""
4149

4250

docs/examples/stream-markdown.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ It'll run the example with both OpenAI and Google Gemini models if the required
44

55
Demonstrates:
66

7-
* streaming text responses
7+
* [streaming text responses](../results.md#streaming-text)
88

99
## Running the Example
1010

docs/examples/stream-whales.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Information about whales — an example of streamed structured response validati
22

33
Demonstrates:
44

5-
* streaming structured responses
5+
* [streaming structured responses](../results.md#streaming-structured-responses)
66

77
This script streams structured responses from GPT-4 about whales, validates the data
88
and displays it as a dynamic table using [`rich`](https://github.com/Textualize/rich) as the data is received.

docs/examples/weather-agent.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Demonstrates:
44

55
* [tools](../agents.md#function-tools)
66
* [agent dependencies](../dependencies.md)
7+
* [streaming text responses](../results.md#streaming-text)
78

89
In this case the idea is a "weather" agent — the user can ask for the weather in multiple locations,
910
the agent will use the `get_lat_lng` tool to get the latitude and longitude of the locations, then use
277 KB
Loading

docs/index.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,14 @@ To understand the flow of the above runs, we can watch the agent in action using
145145

146146
To do this, we need to set up logfire, and add the following to our code:
147147

148-
```py title="bank_support_with_logfire.py"
149-
import logfire
148+
```py title="bank_support_with_logfire.py" hl_lines="4-6"
149+
...
150+
from bank_database import DatabaseConn
150151

152+
import logfire
151153
logfire.configure() # (1)!
152154
logfire.instrument_asyncpg() # (2)!
155+
...
153156
```
154157

155158
1. Configure logfire, this will fail if not project is set up.

docs/install.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ To use Logfire with PydanticAI, install `pydantic-ai` or `pydantic-ai-slim` with
2424
pip/uv-add 'pydantic-ai[logfire]'
2525
```
2626

27-
From there, follow the [Logfire setup docs](logfire.md#integrating-logfire) to configure Logfire.
27+
From there, follow the [Logfire setup docs](logfire.md#using-logfire) to configure Logfire.
2828

2929
## Running Examples
3030

docs/logfire.md

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Monitoring and Performance
1+
# Debugging and Monitoring
22

33
Applications that use LLMs have some challenges that are well known and understood: LLMs are **slow**, **unreliable** and **expensive**.
44

@@ -15,10 +15,10 @@ LLM Observability tools that just let you understand how your model is performin
1515

1616
## Pydantic Logfire
1717

18-
[Pydantic Logfire](https://pydantic.dev/logfire) is an observability platform from the developers of Pydantic and PydanticAI, that aims to let you understand your entire application: Gen AI, classic predictive AI, HTTP traffic, database queries and everything else a modern application needs.
18+
[Pydantic Logfire](https://pydantic.dev/logfire) is an observability platform developed by the team who created and maintain Pydantic and PydanticAI. Logfire aims to let you understand your entire application: Gen AI, classic predictive AI, HTTP traffic, database queries and everything else a modern application needs.
1919

2020
!!! tip "Pydantic Logfire is a commercial product"
21-
Logfire is a commercially supported, hosted platform with an extremely generous and perpetual free tier.
21+
Logfire is a commercially supported, hosted platform with an extremely generous and perpetual [free tier](https://pydantic.dev/pricing/).
2222
You can sign up and start using Logfire in a couple of minutes.
2323

2424
PydanticAI has built-in (but optional) support for Logfire via the [`logfire-api`](https://github.com/pydantic/logfire/tree/main/logfire-api) no-op package.
@@ -29,14 +29,53 @@ Here's an example showing details of running the [Weather Agent](examples/weathe
2929

3030
![Weather Agent Logfire](img/logfire-weather-agent.png)
3131

32-
## Integrating Logfire
32+
## Using Logfire
3333

34-
TODO
34+
To use logfire, you'll need a logfire [account](https://logfire.pydantic.dev), and logfire installed:
3535

36-
## Debugging
36+
```bash
37+
pip/uv-add 'pydantic-ai[logfire]'
38+
```
3739

38-
TODO
40+
Then authenticate your local environment with logfire:
3941

40-
## Monitoring Performance
42+
```bash
43+
py-cli logfire auth
44+
```
4145

42-
TODO
46+
And configure a project to send data to:
47+
48+
```bash
49+
py-cli logfire projects new
50+
```
51+
52+
(Or use an existing project with `logfire projects use`)
53+
54+
The last step is to add logfire to your code:
55+
56+
```python title="adding_logfire.py"
57+
import logfire
58+
59+
logfire.configure()
60+
```
61+
62+
The [logfire documentation](https://logfire.pydantic.dev/docs/) has more details on how to use logfire, including how to instrument other libraries like Pydantic, HTTPX and FastAPI.
63+
64+
Since Logfire is build on [OpenTelemetry](https://opentelemetry.io/), you can use the Logfire Python SDK to send data to any OpenTelemetry collector.
65+
66+
Once you have logfire set up, there are two primary ways it can help you understand your application:
67+
68+
* **Debugging** — Using the live view to see what's happening in your application in real-time.
69+
* **Monitoring** — Using SQL and dashboards to observe the behavior of your application, Logfire is effectively a SQL database that stores information about how your application is running.
70+
71+
### Debugging
72+
73+
To demonstrate how Logfire can let you visualise the flow of a PydanticAI run, here's the view you get from Logfire while running the [chat app examples](examples/chat-app.md):
74+
75+
{{ video('a764aff5840534dc77eba7d028707bfa', 25) }}
76+
77+
### Monitoring Performance
78+
79+
We can also query data with SQL in Logfire to monitor the performance of an application. Here's a real world example of using Logfire to monitor PydanticAI runs inside Logfire itself:
80+
81+
![Logfire monitoring PydanticAI](img/logfire-monitoring-pydanticai.png)

docs/testing-evals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ To get a quantitative measure of performance, we assign points to each run as fo
398398

399399
We use 5-fold cross-validation to judge the performance of the agent using our existing set of examples.
400400

401-
```py title="test_sql_app.py"
401+
```py title="sql_app_evals.py"
402402
import json
403403
import statistics
404404
from pathlib import Path

pydantic_ai_slim/pydantic_ai/agent.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ async def run_stream(
284284
self._result_schema,
285285
deps,
286286
self._result_validators,
287+
lambda m: run_span.set_attribute('all_messages', messages),
287288
)
288289
return
289290
else:

0 commit comments

Comments
 (0)