# On\_agent\_turn\_completed hook

**URL:** <https://community.livekit.io/t/on-agent-turn-completed-hook/1603>\
**Category:** Agents\
**Tags:** agent-development\
**Created:** [July 12, 2026, 4:37pm UTC](https://community.livekit.io/t/on-agent-turn-completed-hook/1603 "2026-07-12T16:37:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isaac\_Huntsman](https://sea1.discourse-cdn.com/flex001/user_avatar/community.livekit.io/isaac_huntsman/32/323_2.png) [@Isaac\_Huntsman](https://community.livekit.io/u/Isaac_Huntsman)\
**Post date:** [July 12, 2026, 4:37pm UTC](https://community.livekit.io/t/on-agent-turn-completed-hook/1603/1 "2026-07-12T16:37:05Z")

</div>

Hey, quick question here. I have a scenario where I have a tool call that outputs something special for agent prose, but the tool call also fires off a call to the database, the payload of which is just a structured JSON version of the agent prose minus the preamble and some formatting. The problem is that I can’t access the structured JSON I generated within the tool call in the @session.on(function\_tools\_executed), I can only access the return value, i.e., the agent prose.

Therefore, I am considering moving away from using session events. The one thing blocking me from doing so, though, is an agent analogue for on\_user\_turn\_completed. I’m feeling really constrained at the moment by Livekit.

Of course there are ways I could “attach” the structured json version of the function tool return value, but they’re all less than ideal, for example, I could use SessionData to transmit it. I could also send in a callback hook in agent init. This is also not ideal.

How should I proceed?

---

<div class="post-metadata">

**Author:** ![CWilson](https://sea1.discourse-cdn.com/flex001/user_avatar/community.livekit.io/cwilson/32/135_2.png) [@CWilson](https://community.livekit.io/u/CWilson)\
**Post date:** [July 13, 2026, 8:50pm UTC](https://community.livekit.io/t/on-agent-turn-completed-hook/1603/2 "2026-07-13T20:50:41Z")

</div>

Hey Isaac — there’s no `on_agent_turn_completed` hook on `Agent` today. The overridable lifecycle hooks are `on_enter`, `on_exit`, `on_user_turn_completed`, and `on_user_turn_exceeded` — “agent turn” only exists internally as a telemetry span. But for what you’re describing, I don’t think you need one. Two options that work with current releases (1.6.x):

**Option 1 — attach the structured JSON to the function call itself.** Inside a tool you have `ctx.function_call`, and `FunctionCall` has a free-form `extra: dict[str, Any]` field. The framework snapshots that call object into `FunctionToolsExecutedEvent.function_calls` _after_ your tool finishes, so anything you stash in `extra` during the tool run is right there in your existing handler, paired with its output:

```python
@function_tool
async def my_tool(ctx: RunContext):
    prose, structured = await build_result()
    await save_to_db(structured)
    ctx.function_call.extra["myapp.structured"] = structured
    return prose

@session.on("function_tools_executed")
def on_tools_executed(ev: FunctionToolsExecutedEvent):
    for call, output in ev.zipped():
        structured = call.extra.get("myapp.structured")
        if structured is not None:
            ... # you have both the prose (output.output) and your JSON

```

Two caveats: namespace your key (provider plugins use `extra` for their own data, e.g. Google thought signatures), and it persists into the chat history / session report, so keep the payload reasonably sized.

**Option 2 — if you specifically want “agent turn completed” semantics, use the speech handle.** `ctx.speech_handle` is the assistant turn your tool is running inside, and `SpeechHandle.add_done_callback()` fires once that whole turn — including the reply the LLM generates from your tool’s return value — has finished playing out. You can close over the structured object directly, no side channel at all:

```python
@function_tool
async def my_tool(ctx: RunContext):
    prose, structured = await build_result()

    def on_turn_done(handle): # handle: SpeechHandle
        asyncio.create_task(save_to_db(structured))

    ctx.speech_handle.add_done_callback(on_turn_done)
    return prose

```

And if you’d rather have it session-wide instead of per-tool: `session.on("speech_created")` hands you every speech handle as it’s created, so adding a done callback there is effectively a global agent-turn-completed hook.

FWIW, your underlying complaint is legitimate: internally the tool executor _does_ keep the raw, pre-serialization return value (`raw_output` on the execution result) — it just isn’t surfaced on `FunctionToolsExecutedEvent`, which only carries the stringified `FunctionCallOutput.output`. Exposing `raw_output` on that event would be a reasonable feature request on the livekit/agents repo. Until then, `extra` is the closest supported channel.

---

<div class="post-metadata">

**Author:** ![Isaac\_Huntsman](https://sea1.discourse-cdn.com/flex001/user_avatar/community.livekit.io/isaac_huntsman/32/323_2.png) [@Isaac\_Huntsman](https://community.livekit.io/u/Isaac_Huntsman)\
**Post date:** [July 13, 2026, 11:36pm UTC](https://community.livekit.io/t/on-agent-turn-completed-hook/1603/3 "2026-07-13T23:36:05Z")

</div>

Yeah option 1 is exactly what I’m looking for and it solves my problem perfectly, thanks!

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex001/user_avatar/community.livekit.io/system/32/26_2.png) [@system](https://community.livekit.io/u/system)\
**Post date:** [July 15, 2026, 11:36pm UTC](https://community.livekit.io/t/on-agent-turn-completed-hook/1603/4 "2026-07-15T23:36:14Z")

</div>

Closed on July 15, 2026, 11:36pm UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
