"""Latency probe: time-to-first-token via Claude Agent SDK (subscription auth)."""
import anyio, time, sys

from claude_agent_sdk import (
    ClaudeSDKClient, ClaudeAgentOptions,
)

MODEL = sys.argv[1] if len(sys.argv) > 1 else "haiku"

async def main():
    opts = ClaudeAgentOptions(
        model=MODEL,
        system_prompt="You are a voice assistant. Reply in one short conversational sentence.",
        max_turns=1,
        allowed_tools=[],          # no tools — pure chat
        include_partial_messages=True,
    )
    async with ClaudeSDKClient(options=opts) as client:
        # Turn 1 (cold: includes CLI spawn + session init)
        for turn in (1, 2, 3):
            t0 = time.monotonic()
            await client.query("Say hi in five words or less.")
            first = None
            text = ""
            async for msg in client.receive_response():
                name = type(msg).__name__
                if name == "StreamEvent":
                    ev = msg.event
                    if ev.get("type") == "content_block_delta":
                        d = ev.get("delta", {})
                        if d.get("type") == "text_delta":
                            if first is None:
                                first = time.monotonic() - t0
                            text += d.get("text", "")
            total = time.monotonic() - t0
            print(f"turn {turn}: first_token={first:.2f}s total={total:.2f}s reply={text!r}")

anyio.run(main)
