# Investigation playbooks

Five investigations an agent is asked for most, each as the user would ask and as the calls the agent makes. They
follow the [Agent quickstart](https://tktail.dev/help/ai-agents-quickstart/) loop; every call is a complete `tools/call` parameter object,
checked against the tools TKTail serves - replace the values in angle brackets with what TKTail returned. The prompts
are examples of what users say; any wording works.

## A request: where did it fail?

> *"Find every record of request R1 and tell me where it failed."*
>
> *"Did a request in payments.log fail in orders.jsonl within 10 seconds?"*

1. **Discover** - the open sources and their formats: does a format have a `requestId` field, or is the id only in the
   text?

   ```json
   { "name": "tktail_list_sources", "arguments": {} }
   ```

2. **Narrow** - one search over every open source: the field where the format has it, the text where it does not.

   ```json
   {
     "name": "tktail_search",
     "arguments": {
       "definition": {
         "sources": [{ "kind": "open" }],
         "predicate": { "any": [
           { "field": { "name": "requestId", "op": "eq", "value": "<request id>" } },
           { "text": "requestId=<request id>" }
         ] }
       },
       "maxResults": 50,
       "withFields": true
     }
   }
   ```

3. **Inspect** - the records around the error result (its `evidenceRef`, or its source and line):

   ```json
   { "name": "tktail_get_records_around", "arguments": { "sourceId": "<payments sourceId>", "line": 2, "before": 5, "after": 5 } }
   ```

4. **Correlate** - the relationship in time, as a sequence: a request received in one log, an error for the same request
   in another within 10 seconds.

   ```json
   {
     "name": "tktail_search",
     "arguments": {
       "definition": {
         "sources": [{ "kind": "source", "path": "<payments sourceId>" }, { "kind": "source", "path": "<orders sourceId>" }],
         "predicate": { "all": [] },
         "sequence": {
           "steps": [
             { "sources": [0], "predicate": { "all": [{ "text": "Request received" }] } },
             { "sources": [1], "predicate": { "all": [{ "field": { "name": "level", "op": "eq", "value": "ERROR" } }] },
               "window": { "after": "previous", "earliestMs": 0, "latestMs": 10000 } }
           ],
           "correlation": [
             { "name": "requestId", "compare": "exact", "steps": [
               { "input": "message", "extract": "after", "text": "requestId=" },
               { "input": "field", "field": "requestId" }
             ] }
           ],
           "policy": "earliest"
         }
       }
     }
   }
   ```

5. **Report** - each sequence's members in order with their times and sources; say if the search was incomplete.

## An alert: what happened, and watching for it

> *"What happened in the last Payment errors incident?"*

1. The alerts and the newest incident of one of them:

   ```json
   { "name": "tktail_list_alerts", "arguments": {} }
   ```

   ```json
   { "name": "tktail_list_alert_occurrences", "arguments": { "alertNumber": 3, "limit": 1 } }
   ```

2. The incident and the evidence it kept - the trigger record, the records before and after it, events, metric
   samples - each item with its own state:

   ```json
   { "name": "tktail_get_alert_occurrence", "arguments": { "occurrenceId": "<occurrenceId>" } }
   ```

   ```json
   { "name": "tktail_get_occurrence_evidence", "arguments": { "occurrenceId": "<occurrenceId>", "item": 1, "limit": 20 } }
   ```

3. Report what fired, when, and what the evidence shows; an item that is `partial` or `failed` says why.

> *"Watch orders.jsonl for ERROR records and keep the records around each one."*

Saving needs the user's permissions *Allow AI to save searches and Metric Sets* and *Allow AI to manage alerts*;
without them, validate and give the alert to the user to create in Manage Alerts.

1. Check the alert with TKTail - this writes nothing and needs no permission:

   ```json
   {
     "name": "tktail_validate_alert",
     "arguments": {
       "alert": {
         "name": "Order errors",
         "severity": "error",
         "condition": {
           "sources": [{ "kind": "source", "path": "<orders sourceId>" }],
           "predicate": { "all": [{ "field": { "name": "level", "op": "eq", "value": "ERROR" } }] }
         },
         "trigger": { "kind": "everyMatch" },
         "actions": ["notify"],
         "capture": { "items": [{ "kind": "file", "recordsBefore": 5, "recordsAfter": 5 }] }
       }
     }
   }
   ```

2. With the user's consent, save the same `alert` with `tktail_save_alert`, and the condition as a Saved Search:

   ```json
   {
     "name": "tktail_save_search",
     "arguments": {
       "name": "Order errors",
       "definition": {
         "sources": [{ "kind": "source", "path": "<orders sourceId>" }],
         "predicate": { "all": [{ "field": { "name": "level", "op": "eq", "value": "ERROR" } }] }
       }
     }
   }
   ```

3. To change a saved one later, read it first (`tktail_get_alert`, `tktail_get_saved_search`) and pass its `version` as
   `expectedVersion` - a change to something the user edited since is refused, never overwritten.

## An unknown format: why is this plain text?

> *"Why is this log shown as plain text? Write a format for it."*

1. How TKTail chose the format, and what the detector saw:

   ```json
   { "name": "tktail_detect_format", "arguments": { "sourceId": "<sourceId>" } }
   ```

2. A sample of the log's records as TKTail frames them:

   ```json
   { "name": "tktail_get_source_sample", "arguments": { "sourceId": "<sourceId>", "records": 20 } }
   ```

3. A draft profile fitted to the log (`delimited` for CSV-like logs, `lineBased` or `multiline` otherwise), then the
   profile with the fields named and given their roles - checked, and previewed on the log's own records until every
   record parses:

   ```json
   { "name": "tktail_new_format_draft", "arguments": { "sourceId": "<sourceId>", "template": "lineBased" } }
   ```

   ```json
   {
     "name": "tktail_validate_format",
     "arguments": {
       "definition": {
         "schemaVersion": 1,
         "id": "orders.worker",
         "name": "Orders worker",
         "framing": { "kind": "line" },
         "envelope": { "pattern": "^(?<timestamp>\\S+ \\S+) (?<level>[A-Z]+) (?<body>.*)$" },
         "fields": [
           { "id": "timestamp", "group": "timestamp", "type": "timestamp", "role": "timestamp" },
           { "id": "level", "group": "level", "role": "severity" },
           { "id": "body", "group": "body", "role": "message" }
         ]
       }
     }
   }
   ```

   A `delimited` draft has the log's delimiter, quote and header and one field per column (`columns` lists them,
   1-based): give the time column `"role": "timestamp"` and `"type": "timestamp"`, the others `"role": "severity"`,
   `"role": "message"`, and rename ids freely. `tktail_preview_format` takes the same `definition` with the `sourceId`
   and shows each record as the profile parses it.

4. With the user's consent and *Allow AI to save custom formats*: `tktail_save_format` with the `definition` and
   `applyToSourceId`; to have the file use it after a restart, put its file name in `definition.fileMatch.names` before
   saving (`["export-*.csv"]`). Without the permission, give the profile to the user to import in the Custom Format
   Studio ([Your own log formats](https://tktail.dev/help/custom-formats/)).

## Performance: what did the machine do?

> *"What did CPU, memory and disk do in the minute around this error?"*

Metrics are included with Trial and Pro, and TKTail reports only what it collects: a Metric Set or a Metrics View must
be open (**View > Metrics...**) - the agent never starts collection.

1. The metrics TKTail collects or still keeps:

   ```json
   { "name": "tktail_list_metrics", "arguments": {} }
   ```

2. Minimum, average and maximum around the error's time, the peak with its time, and every gap:

   ```json
   { "name": "tktail_get_metric_summary", "arguments": { "around": { "time": "2026-10-08T10:00:01", "seconds": 60 } } }
   ```

3. The samples of one metric over the same interval, if the summary shows a peak worth a closer look:

   ```json
   { "name": "tktail_get_metric_history", "arguments": { "metricId": "<metricId>", "around": { "time": "2026-10-08T10:00:01", "seconds": 60 } } }
   ```

4. Report the values with the metric's unit and say where samples were missing - a reading TKTail could not take is
   missing, never zero.

## Evidence: keep it before the log rotates

> *"Save these results with 20 records of context before the log rotates."*
>
> *"Pin this search and preserve its evidence - include orders.jsonl for the same seconds."*

A busy log may keep only minutes of history. Preserve what explains the problem as soon as it is found - never copy a
whole log with a shell command. Saving and pinning need *Allow AI to save evidence and pin searches* (off until the user turns it on - a refusal
`not_allowed` says so: ask the user); TKTail redacts
secrets before anything is written, and everything read back is the redacted package.

1. The search whose results are the evidence (or a `searchId` the investigation already has):

   ```json
   { "name": "tktail_search", "arguments": { "definition": { "sources": [{ "kind": "open" }], "predicate": { "all": [{ "text": "Payment failed" }] } }, "maxResults": 20 } }
   ```

2. Pin it while the investigation goes on - kept across restarts and outside the limit of five AI searches (a pin keeps
   references; only a package keeps the bytes):

   ```json
   { "name": "tktail_pin_search", "arguments": { "searchId": 19 } }
   ```

3. Preserve: the hits' records with 20 records on each side (a stack trace is one record), and the other log for the
   same seconds. The answer names the package, its SHA-256, the time range and how many secrets were redacted; a source
   that rotated before it could be read is reported, never guessed:

   ```json
   {
     "name": "tktail_save_evidence",
     "arguments": {
       "searchId": 19,
       "contextRecordsBefore": 20,
       "contextRecordsAfter": 20,
       "contextTimeBeforeMs": 15000,
       "contextTimeAfterMs": 15000,
       "correlatedSourceIds": ["<orders sourceId>"],
       "note": "payment timeouts before the outage"
     }
   }
   ```

   The capture runs in the background. **`state: capturing` (`preserved: false`) is NOT saved yet**: call
   `tktail_get_evidence` with the package id (it waits up to `waitMs`) until the state is `saved` - only then tell the
   user the evidence is preserved. `failed` or `cancelled` (`preserved: false`, a `failureCode` such as
   `evidence_quota_exceeded`, and the reason) means nothing was kept: say so, fix the cause (a narrower search, room in
   the store) and save again - a new capture with a new package id. `tktail_list_evidence` lists recent failed
   captures under `failed`, so a failure is found even after the package id is forgotten. A package keeps at most 5,000
   hits (as many as a search stores).

4. Check what was kept - the manifest summary, then a page of the parsed records of the first source (`fields` returns
   only the fields named):

   ```json
   { "name": "tktail_get_evidence", "arguments": { "packageId": "<packageId>", "maxHits": 10 } }
   ```

   ```json
   { "name": "tktail_get_evidence", "arguments": { "packageId": "<packageId>", "artifact": "sources/source-001/records.jsonl", "maxBytes": 4096 } }
   ```

5. Verify before it is shared - VALID, MODIFIED, INCOMPLETE or UNSUPPORTED, also after the original log is gone:

   ```json
   { "name": "tktail_verify_evidence", "arguments": { "packageId": "<packageId>" } }
   ```

6. Report the package id, its SHA-256, the time range and the redactions; the user makes a shareable copy or links it
   to an incident in the Evidence Library ([Saving evidence](https://tktail.dev/help/saving-evidence/)). Deleting a package is a request
   (`tktail_delete_evidence`) the user confirms - ask first.

---

The HTML page: https://tktail.dev/help/ai-agents-playbooks/
