CREATE TABLE IF NOT EXISTS agent_trace (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    invocation UUID NOT NULL REFERENCES agent_model_invocation(id),
    conversation UUID NOT NULL,
    sequence INT NOT NULL,
    phase TEXT NOT NULL CHECK (phase IN ('think', 'answer', 'act', 'summarize')),
    content TEXT,
    tool_calls JSONB,
    tool_results JSONB,
    input_tokens BIGINT,
    output_tokens BIGINT,
    metadata JSONB,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_agent_trace_invocation ON agent_trace(invocation);
CREATE INDEX IF NOT EXISTS idx_agent_trace_conversation ON agent_trace(conversation, sequence);
