Skip to main content

Module session

Module session 

Source
Expand description

Multi-turn session management.

A Session threads Claude’s session_id across turns automatically, so callers never need to scrape it out of a result event or pass --resume by hand.

§Ownership

Session holds an Arc<Claude>. Wrapping the client in an Arc means a session can outlive the original client binding, be moved between tasks, and sit inside long-lived actor state – which is the usage shape that callers like centralino-rs need. One Arc::clone per session is a negligible cost in exchange.

§Two entry points

  • Session::send takes a plain prompt. Use this for straightforward multi-turn chat.
  • Session::execute takes a fully-configured QueryCommand. Use this when you want per-turn options like model, max_turns, permission_mode, etc. The session automatically overrides any session-related flags on the command (--resume, --continue, --session-id, --fork-session) so they can’t conflict.

Streaming follows the same split: Session::stream and Session::stream_execute.

§Example

use std::sync::Arc;
use claude_wrapper::{Claude, QueryCommand};
use claude_wrapper::session::Session;

let claude = Arc::new(Claude::builder().build()?);

let mut session = Session::new(Arc::clone(&claude));

// Simple path
let first = session.send("explain quicksort").await?;

// Full control: custom model, effort, permission mode, etc.
let second = session
    .execute(QueryCommand::new("now mergesort").model("opus"))
    .await?;

println!("total cost: ${:.4}", session.total_cost_usd());
println!("turns: {}", session.total_turns());

§Resuming an existing session

// Reattach to a session you stored earlier
let mut session = Session::resume(claude, "sess-abc123");
let result = session.send("pick up where we left off").await?;

Structs§

Session
A multi-turn conversation handle.