Skip to content

Commit d5d16ef

Browse files
authored
Disable instrumentation by default, add instrument param to Agent, use plain OpenTelemetry (#1005)
1 parent d2a6cd4 commit d5d16ef

File tree

20 files changed

+223
-173
lines changed

20 files changed

+223
-173
lines changed

docs/index.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,29 @@ To understand the flow of the above runs, we can watch the agent in action using
168168

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

171-
```python {title="bank_support_with_logfire.py" hl_lines="4-6" test="skip" lint="skip"}
171+
```python {title="bank_support_with_logfire.py" hl_lines="6-8 20" test="skip" lint="skip"}
172172
...
173+
from pydantic_ai import Agent, RunContext
174+
173175
from bank_database import DatabaseConn
174176

175177
import logfire
178+
176179
logfire.configure() # (1)!
177180
logfire.instrument_asyncpg() # (2)!
181+
178182
...
183+
184+
support_agent = Agent(
185+
'openai:gpt-4o',
186+
deps_type=SupportDependencies,
187+
result_type=SupportResult,
188+
system_prompt=(
189+
'You are a support agent in our bank, give the '
190+
'customer support and judge the risk level of their query.'
191+
),
192+
instrument=True,
193+
)
179194
```
180195

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

docs/logfire.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,27 @@ py-cli logfire projects new
5151

5252
(Or use an existing project with `logfire projects use`)
5353

54-
The last step is to add logfire to your code:
54+
Then add logfire to your code:
5555

5656
```python {title="adding_logfire.py"}
5757
import logfire
5858

5959
logfire.configure()
6060
```
6161

62+
and enable instrumentation in your agent:
63+
64+
```python {title="instrument_agent.py"}
65+
from pydantic_ai import Agent
66+
67+
agent = Agent('openai:gpt-4o', instrument=True)
68+
```
69+
6270
The [logfire documentation](https://logfire.pydantic.dev/docs/) has more details on how to use logfire,
6371
including how to instrument other libraries like [Pydantic](https://logfire.pydantic.dev/docs/integrations/pydantic/),
6472
[HTTPX](https://logfire.pydantic.dev/docs/integrations/http-clients/httpx/) and [FastAPI](https://logfire.pydantic.dev/docs/integrations/web-frameworks/fastapi/).
6573

66-
Since Logfire is build on [OpenTelemetry](https://opentelemetry.io/), you can use the Logfire Python SDK to send data to any OpenTelemetry collector.
74+
Since Logfire is built on [OpenTelemetry](https://opentelemetry.io/), you can use the Logfire Python SDK to send data to any OpenTelemetry collector.
6775

6876
Once you have logfire set up, there are two primary ways it can help you understand your application:
6977

@@ -105,10 +113,10 @@ from pydantic_ai import Agent
105113
logfire.configure()
106114
logfire.instrument_httpx(capture_all=True) # (1)!
107115

108-
agent = Agent('openai:gpt-4o')
116+
agent = Agent('openai:gpt-4o', instrument=True)
109117
result = agent.run_sync('What is the capital of France?')
110118
print(result.data)
111-
#> The capital of France is Paris.
119+
# > The capital of France is Paris.
112120
```
113121

114122
1. Capture all of headers, request body, and response body.

examples/pydantic_ai_examples/chat_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# 'if-token-present' means nothing will be sent (and the example will work) if you don't have logfire configured
4040
logfire.configure(send_to_logfire='if-token-present')
4141

42-
agent = Agent('openai:gpt-4o')
42+
agent = Agent('openai:gpt-4o', instrument=True)
4343
THIS_DIR = Path(__file__).parent
4444

4545

examples/pydantic_ai_examples/flight_booking.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Deps:
4949
system_prompt=(
5050
'Your job is to find the cheapest flight for the user on the given date. '
5151
),
52+
instrument=True,
5253
)
5354

5455

examples/pydantic_ai_examples/pydantic_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MyModel(BaseModel):
2525

2626
model = cast(KnownModelName, os.getenv('PYDANTIC_AI_MODEL', 'openai:gpt-4o'))
2727
print(f'Using model: {model}')
28-
agent = Agent(model, result_type=MyModel)
28+
agent = Agent(model, result_type=MyModel, instrument=True)
2929

3030
if __name__ == '__main__':
3131
result = agent.run_sync('The windy city in the US of A.')

examples/pydantic_ai_examples/question_graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# 'if-token-present' means nothing will be sent (and the example will work) if you don't have logfire configured
2323
logfire.configure(send_to_logfire='if-token-present')
2424

25-
ask_agent = Agent('openai:gpt-4o', result_type=str)
25+
ask_agent = Agent('openai:gpt-4o', result_type=str, instrument=True)
2626

2727

2828
@dataclass

examples/pydantic_ai_examples/rag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Deps:
4848
pool: asyncpg.Pool
4949

5050

51-
agent = Agent('openai:gpt-4o', deps_type=Deps)
51+
agent = Agent('openai:gpt-4o', deps_type=Deps, instrument=True)
5252

5353

5454
@agent.tool

examples/pydantic_ai_examples/roulette_wheel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class Deps:
2626
retries=3,
2727
result_type=bool,
2828
system_prompt=(
29-
'Use the `roulette_wheel` function to determine if the '
30-
'customer has won based on the number they bet on.'
29+
'Use the `roulette_wheel` function to determine if the customer has won based on the number they bet on.'
3130
),
31+
instrument=True,
3232
)
3333

3434

examples/pydantic_ai_examples/sql_gen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class InvalidRequest(BaseModel):
9797
# Type ignore while we wait for PEP-0747, nonetheless unions will work fine everywhere else
9898
result_type=Response, # type: ignore
9999
deps_type=Deps,
100+
instrument=True,
100101
)
101102

102103

examples/pydantic_ai_examples/stream_markdown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# 'if-token-present' means nothing will be sent (and the example will work) if you don't have logfire configured
2222
logfire.configure(send_to_logfire='if-token-present')
2323

24-
agent = Agent()
24+
agent = Agent(instrument=True)
2525

2626
# models to try, and the appropriate env var
2727
models: list[tuple[KnownModelName, str]] = [

0 commit comments

Comments
 (0)