What's the best way to get call duration?

This question originally came up in our Slack community and the thread has been consolidated here for long-term reference.

Is this a good approach to get the duration of the call, or is there a better approach?

@session.on("close")
def on_close(ev: CloseEvent):
    logger.info(f"Room creation: {ctx.room.creation_time}")
    logger.info(f"Room closed at: {datetime.fromtimestamp(ev.created_at)}")

Your approach works for calculating duration within the agent.

For cloud-hosted agents, you can also use the Analytics API which provides detailed session and participant information:

The Analytics API is useful for post-hoc analysis and reporting without needing to capture data in your agent code.