> For the complete documentation index, see [llms.txt](https://ai.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai.ortusbooks.com/main-components/middleware/flight-recorder.md).

# FlightRecorderMiddleware

Record and replay LLM and tool interactions to deterministic JSON fixtures

Class: `bxModules.bxai.models.middleware.core.FlightRecorderMiddleware`

Records structured LLM/tool interactions to fixture files and can replay them without live provider or tool execution.

## Features

* Three modes: `passthrough`, `record`, `replay`
* Captures LLM requests/responses and optional tool args/results
* Incremental snapshot writes for crash-safe recordings
* Strict or lenient replay matching
* Public inspection helpers: `getTape()`, `getEffectiveFixturePath()`, `reset()`

## Constructor

```javascript
middleware = new bxModules.bxai.models.middleware.core.FlightRecorderMiddleware(
    mode       : "record",
    fixturePath: "",
    fixtureDir : "/.agents/flight-recorder",
    recordTools: true,
    strict     : true
)
```

## Configuration

| Option        | Type    | Default                      | Description                                           |
| ------------- | ------- | ---------------------------- | ----------------------------------------------------- |
| `mode`        | string  | `"passthrough"`              | Operating mode: `passthrough`, `record`, `replay`     |
| `fixturePath` | string  | `""`                         | Explicit fixture path (required in replay mode)       |
| `fixtureDir`  | string  | `"/.agents/flight-recorder"` | Directory used when auto-generating fixture files     |
| `recordTools` | boolean | `true`                       | Record/replay tool interactions                       |
| `strict`      | boolean | `true`                       | Enforce exact interaction type sequence during replay |

## Hooks Used

* `beforeAgentRun`
* `afterAgentRun`
* `wrapLLMCall`
* `wrapToolCall`

## Example

```javascript
agent = aiAgent(
    name      : "weather-agent",
    middleware: [
        new bxModules.bxai.models.middleware.core.FlightRecorderMiddleware(
            mode       : "record",
            fixtureDir : "/.agents/flight-recorder",
            recordTools: true
        )
    ]
)
```

```javascript
agent = aiAgent(
    middleware: [
        new bxModules.bxai.models.middleware.core.FlightRecorderMiddleware(
            mode       : "replay",
            fixturePath: "tests/fixtures/weather-agent.json",
            strict     : true
        )
    ]
)
```

## Notes

* In replay mode, missing fixture files throw `FlightRecorder.FixtureNotFound`.
* Strict mode throws on interaction-type mismatch; lenient mode scans forward for next matching type.
* Auto-generated file names are normalized from agent name plus timestamp.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ai.ortusbooks.com/main-components/middleware/flight-recorder.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
