# AI use cases for Logfire (pydantic/logfire)

Repo: [pydantic/logfire](https://github.com/pydantic/logfire) | Use cases: 3 | Page: https://dedupedai.com/use-cases/pydantic-logfire/

Logfire is a Python SDK and CLI for observability: manual tracing with spans and logs, plus ready integrations such as FastAPI. The SDK and docs are open source; the server that records and displays data is closed source.

This file holds every use case of this repo. Each one starts with a "## " heading and ends with its URL.

- Connect a Python project to Logfire in two commands: https://dedupedai.com/use-cases/pydantic-logfire/connect-a-python-project-to-logfire/
- Trace a script with spans and structured logs: https://dedupedai.com/use-cases/pydantic-logfire/trace-a-script-with-logfire-spans/
- Instrument a FastAPI app without manual tracing: https://dedupedai.com/use-cases/pydantic-logfire/instrument-a-fastapi-app-with-logfire/

## How to connect a Python project to Logfire in two commands with Logfire

Repo: [pydantic/logfire](https://github.com/pydantic/logfire) | Difficulty: Beginner | Time: 10 minutes | Basis: From the README

Install the SDK and CLI, authenticate, and let init pick or create the project and save its write token.

**Who it is for:** A developer adding observability to an existing Python service.

**The problem:** Wiring tokens and projects by hand is fiddly.

**The outcome:** A project linked to Logfire and ready to receive traces.

### Steps

1. Install with `pip install logfire`
2. From your application's directory run `uvx logfire-cli auth` to sign in
3. Run `uvx logfire-cli init` to select or create a project and save its write token locally
4. Call Logfire.configure() in your code, which does not prompt for setup

### Prompt to paste into your agent

```text
Using pydantic/logfire, install it with `pip install logfire`, run `uvx logfire-cli auth` and `uvx logfire-cli init` in <my app directory>, then add logfire.configure() to the entrypoint.
```

### Caveat

The server application is closed source; only the SDK and docs are in the repo.

Risk, credentials: init saves a project write token locally; keep it out of git.

Risk, data: Traces you send go to the hosted Logfire backend, which is closed source.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/pydantic-logfire/connect-a-python-project-to-logfire/

## How to trace a script with spans and structured logs with Logfire

Repo: [pydantic/logfire](https://github.com/pydantic/logfire) | Difficulty: Beginner | Time: 15 minutes | Basis: From the README

Use Logfire.info and Logfire.span to record named, parameterised events around a block of work.

**Who it is for:** A developer debugging slow or odd behaviour in a script.

**The problem:** Plain print statements lose structure and timing.

**The outcome:** Structured logs and a timed span for the block you wrapped.

### Steps

1. Install with `pip install logfire`
2. Add import Logfire and call Logfire.configure()
3. Log with Logfire.info('Hello, {name}!', name='world')
4. Wrap work in with Logfire.span('Asking the user their {question}', question='age'): and log inside it with Logfire.debug

### Prompt to paste into your agent

```text
With pydantic/logfire, add logfire.configure(), a logfire.info call and a logfire.span around the main function of <my script>, then explain what each field in the output means.
```

### Caveat

You need a connected Logfire project to view the results.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/pydantic-logfire/trace-a-script-with-logfire-spans/

## How to instrument a FastAPI app without manual tracing with Logfire

Repo: [pydantic/logfire](https://github.com/pydantic/logfire) | Difficulty: Intermediate | Time: 20 minutes | Basis: From the README

Call Logfire.instrument_fastapi on your app to trace requests automatically.

**Who it is for:** A backend developer running a FastAPI service.

**The problem:** Adding spans to every endpoint by hand does not scale.

**The outcome:** Request traces for every route of your FastAPI app.

### Steps

1. Install with `pip install logfire`
2. Create your FastAPI app as usual
3. Call Logfire.configure() and then Logfire.instrument_fastapi(app)
4. Instrument your database connector and http library the same way, as the README suggests

### Prompt to paste into your agent

```text
Using pydantic/logfire, add logfire.configure() and logfire.instrument_fastapi(app) to <my FastAPI app> and list the other libraries I should instrument next.
```

### Caveat



Risk, data: Request data is sent to the hosted backend; check what your routes carry first.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/pydantic-logfire/instrument-a-fastapi-app-with-logfire/
