> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flowra.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat streaming

> Stream Graphify chat runs with the Flowra SDKs and parse SSE usage events — with full examples.

Agent and chat runs stream over SSE via `POST /api/v1/graphify/threads/{thread_id}/runs/stream`. Both SDKs wrap this as `chat.stream` and ship helpers to parse `event: usage` payloads.

Concepts: [Threads](/guides/threads). Credits: [Usage and credits](/guides/usage-and-credits).

Created threads use LangGraph’s **`thread_id`** field (not `id`).

## End-to-end: create thread and stream

<CodeGroup>
  ```ts theme={null}
  import { Flowra, parseSseChunk, extractStreamUsage } from '@flowra/sdk';

  const flowra = new Flowra({ apiKey: process.env.FLOWRA_API_KEY! });

  const thread = await flowra.chat.createThread({
    metadata: { source: 'docs-example' },
  });
  const threadId = thread.thread_id as string;

  const body = {
    input: {
      messages: [{ role: 'user', content: 'List my open support themes in one paragraph.' }],
    },
  };

  const result = await flowra.chat.stream(threadId, body);

  // If your transport gives you raw SSE text, parse it:
  if (typeof result === 'string') {
    const { events } = parseSseChunk(result.endsWith('\n\n') ? result : `${result}\n\n`);
    const usage = extractStreamUsage(events);
    if (usage) {
      console.log('This turn:', usage.runCredits, 'credits');
    }
  }
  ```

  ```python theme={null}
  from flowra import Flowra, extract_stream_usage

  flowra = Flowra(api_key="YOUR_API_KEY")

  thread = flowra.chat.create_thread({"metadata": {"source": "docs-example"}})
  thread_id = thread["thread_id"]

  body = {
      "input": {
          "messages": [
              {
                  "role": "user",
                  "content": "List my open support themes in one paragraph.",
              }
          ]
      }
  }

  for event in flowra.chat.stream(thread_id, body, as_events=True):
      kind = event["event"]
      data = event.get("data")
      if kind == "usage":
          print("Credits this turn:", data.get("runCredits"))
      else:
          print(kind, data)
  ```
</CodeGroup>

## Parse SSE chunks (shared helpers)

TypeScript:

```ts theme={null}
import { parseSseChunk, extractStreamUsage } from '@flowra/sdk';

let buffer = '';
for (const chunk of sseTextChunks) {
  buffer += chunk;
  const { events, remainder } = parseSseChunk(buffer);
  buffer = remainder;
  const usage = extractStreamUsage(events);
  if (usage) {
    console.log(usage.runCredits, usage.threadCreditsTotal, usage.balanceRemaining);
  }
}
```

Python (buffered body):

```python theme={null}
from flowra import parse_sse_chunk, extract_stream_usage

raw = flowra.chat.stream(thread_id, body)  # full SSE text when as_events=False
events, _ = parse_sse_chunk(raw if raw.endswith("\n\n") else raw + "\n\n")
usage = extract_stream_usage(events)
```

## Inspect history, state, and runs

<CodeGroup>
  ```ts theme={null}
  await flowra.chat.history(threadId, {});
  await flowra.chat.state(threadId);
  const runs = await flowra.chat.listRuns(threadId);
  // await flowra.chat.cancelRun(threadId, runId);
  await flowra.chat.searchThreads({ metadata: { source: 'docs-example' } });
  ```

  ```python theme={null}
  flowra.chat.history(thread_id, {})
  flowra.chat.state(thread_id)
  runs = flowra.chat.list_runs(thread_id)
  # flowra.chat.cancel_run(thread_id, run_id)
  flowra.chat.search_threads({"metadata": {"source": "docs-example"}})
  ```
</CodeGroup>

## Other chat helpers

| TypeScript                                    | Python                                           |
| --------------------------------------------- | ------------------------------------------------ |
| `createThread`                                | `create_thread`                                  |
| `getThread` / `updateThread` / `deleteThread` | `get_thread` / `update_thread` / `delete_thread` |
| `searchThreads` / `history` / `state`         | `search_threads` / `history` / `state`           |
| `listRuns` / `cancelRun`                      | `list_runs` / `cancel_run`                       |

## Related

* [Threads](/guides/threads)
* [Usage and credits](/guides/usage-and-credits)
* [TypeScript SDK](/guides/sdk-typescript)
* [Python SDK](/guides/sdk-python)
* [Human in the loop](/guides/human-in-the-loop)
