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

# Add an agent second pass

> Run a Corti agent automatically over a finalized transcript to produce a second-pass result

<iframe className="w-full aspect-video rounded-xl" src="https://www.youtube.com/embed/0I2LVQsyemg?start=48&end=98" title="Add an Agent Second Pass — walkthrough" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowFullScreen />

<Info>
  **Follow along with the runnable example.** This guide walks through the [second-pass agent example](https://github.com/corticph/corti-examples/tree/main/sdk/typescript/applets/src/applets/second-pass-agent) in `corti-examples` — bootstrap your build from it or use it as reference.
</Info>

Where the [on-demand agent](/stt/guides/agent-on-demand) runs when the user asks, a **second-pass agent** runs *automatically* as the final step of a transcription pipeline. Generate an offline transcript with [`/transcripts`](/stt/guides/dictation-file-transcription), then feed the finalized result to an [Agentic Framework](/agentic/overview) agent — for example to produce a concise clinical summary from a diarized conversation.

<Note>
  This guide chains two earlier ones: the [File Transcription](/stt/guides/dictation-file-transcription) pipeline and the agent lifecycle from [On-Demand Agent](/stt/guides/agent-on-demand) (create-by-name, isolated `messageSend`, prompt-driven behavior).

  The prompts used here are mere examples - be sure to test and refine the prompt to support your specific needs.
</Note>

***

## Chain the second pass

Run the pipeline to completion, then hand the finalized transcript to the agent as a second stage. Keep them as distinct steps so the transcript is the source of truth and the agent run is an augmentation on top of it.

```ts title="JavaScript" theme={null}
// 1. Generate the offline transcript (see the File Transcription guide).
const transcript = await generateTranscript(/* interaction, recording, params */);

// 2. Second pass: send the finalized transcript to the agent.
const agentInput = flattenTranscriptForAgent(transcript);
const secondPass = await sendAgentMessage(client, agentId, agentInput);
```

***

## Prepare the agent input

A `/transcripts` result is a set of segments, not a flat string. Flatten it for the agent — and prefix each segment with its **speaker or channel label** so the agent can reason about who said what.

```ts title="JavaScript" theme={null}
function flattenTranscriptForAgent(transcript): string {
  return getSegments(transcript)
    .map((s) => {
      const label =
        typeof s.speakerId === "number" ? `Speaker ${s.speakerId}`
        : typeof s.channel === "number"  ? `Channel ${s.channel}`
        : null;
      return label ? `${label}: ${s.text}` : s.text;
    })
    .join("\n");
}
```

The second-pass behavior lives in the agent's `systemPrompt`:

```ts title="Second-pass system prompt" theme={null}
const SECOND_PASS = {
  name: "Transcript Second Pass",
  description: "Second-pass processor for finalized transcripts.",
  systemPrompt: `You are a second-pass processor for healthcare transcripts.
You will receive transcript text that may include speaker or channel labels.
Produce a concise, clinically useful output from that transcript.
Preserve important medications, numbers, units, timing, and uncertainty.
Return only the final result with no preamble, labels, or markdown fences.`,
};
```

***

## Keep the transcript on agent failure

The transcript and the agent pass are independent results. If the second pass errors — a `403`, a timeout, an empty response — **preserve the finalized transcript** and surface the agent error separately. Never discard a good transcript because the augmentation failed.

<Tip>
  Treat the second pass as an optional stage layered on the transcript pipeline: the transcript always renders, and the agent output appears when (and if) it succeeds. Store the prompt per API client so it can be tuned without a code change.
</Tip>

***

## Next steps

<CardGroup cols={2}>
  <Card title="File Transcription" icon="file-audio" href="/stt/guides/dictation-file-transcription">
    The `/transcripts` pipeline this second pass runs on top of.
  </Card>

  <Card title="On-Demand Agent" icon="robot" href="/stt/guides/agent-on-demand">
    The agent lifecycle and message API, triggered manually instead.
  </Card>

  <Card title="Example code" icon="github" href="https://github.com/corticph/corti-examples/tree/main/sdk/typescript/applets/src/applets/second-pass-agent">
    The complete, runnable second-pass agent example in `corti-examples`.
  </Card>
</CardGroup>

<Note>Please [contact us](mailto:help@corti.ai) for help or questions.</Note>
