# llm-prompt-json

A plugin for [LLM](https://llm.datasette.io/) that adds a `--json` flag to the `llm prompt` command. This flag allows you to retrieve the response, conversation ID, and metadata as a JSON array, making it easy to integrate with tools like `jq`.

Built for `llm` version 0.28.

## Installation

Install this plugin in the same environment as LLM:

```bash
llm install llm-prompt-json
```

## Usage

Use the `--json` flag when running a prompt:

```bash
llm "say hello" --json
```

Example output:

```json
[
  {
    "id": "01...",
    "model": "gpt-4o",
    "prompt": "say hello",
    "response": "Hello!",
    "conversation_id": "01..."
  }
]
```

### Extracting the Conversation ID

Because the output is standard JSON, you can easily pipe it into `jq` to extract just the values you need. For example, to get the `conversation_id`:

```bash
llm "say hello" --json | jq -r '.[].conversation_id'
```

## Compatibility

This plugin intercepts `llm prompt` dynamically, preventing the usual streamed output from polluting the JSON payload. It works correctly even if streaming is enabled or disabled, but it cannot be used alongside the `--no-log` flag since it relies on the internal LLM logs database to construct the complete metadata record.
