Copy
const span = await tracia.spans.get(spanId);
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
spanId | string | Yes | The span ID (e.g., sp_abc123def456) |
Response
Copy
interface Span {
id: string;
spanId: string;
traceId: string | null;
parentSpanId: string | null;
promptSlug: string | null;
promptVersion: number | null;
model: string;
provider: string;
input: { messages: PromptMessage[] };
variables: Record<string, string> | null;
output: string | null;
status: 'SUCCESS' | 'ERROR';
error: string | null;
latencyMs: number;
inputTokens: number;
outputTokens: number;
totalTokens: number;
cost: number | null;
tags: string[];
userId: string | null;
sessionId: string | null;
createdAt: string;
}
Examples
Basic Usage
Copy
const span = await tracia.spans.get('sp_abc123def456');
console.log(`Model: ${span.model} (${span.provider})`);
console.log(`Status: ${span.status}`);
console.log(`Latency: ${span.latencyMs}ms`);
Viewing Input and Output
Copy
const span = await tracia.spans.get('sp_abc123def456');
console.log('Input messages:');
span.input.messages.forEach(msg => {
console.log(` [${msg.role}]: ${msg.content.slice(0, 100)}...`);
});
console.log('\nOutput:');
console.log(span.output);
Checking Variables Used
Copy
const span = await tracia.spans.get('sp_abc123def456');
if (span.variables) {
console.log('Variables used:');
Object.entries(span.variables).forEach(([key, value]) => {
console.log(` ${key}: ${value}`);
});
}
Checking Trace Session
Copy
const span = await tracia.spans.get('sp_abc123def456');
if (span.traceId) {
console.log(`Part of trace session: ${span.traceId}`);
}
if (span.parentSpanId) {
console.log(`Parent span: ${span.parentSpanId}`);
}
Handling Errors
Copy
const span = await tracia.spans.get('sp_abc123def456');
if (span.status === 'ERROR') {
console.error(`Span failed: ${span.error}`);
} else {
console.log('Span succeeded');
console.log(`Tokens: ${span.inputTokens} in, ${span.outputTokens} out`);
console.log(`Cost: $${span.cost?.toFixed(4) || 'N/A'}`);
}
Debugging a Prompt Run
Copy
const span = await tracia.spans.get('sp_abc123def456');
console.log('=== Span Debug Info ===');
console.log(`Span ID: ${span.spanId}`);
console.log(`Trace ID: ${span.traceId || 'N/A'}`);
console.log(`Parent Span: ${span.parentSpanId || 'N/A'}`);
console.log(`Prompt: ${span.promptSlug || 'runLocal'} v${span.promptVersion || 'N/A'}`);
console.log(`Model: ${span.model} (${span.provider})`);
console.log(`Status: ${span.status}`);
console.log(`Latency: ${span.latencyMs}ms`);
console.log(`Tokens: ${span.inputTokens} + ${span.outputTokens} = ${span.totalTokens}`);
console.log(`Cost: $${span.cost?.toFixed(4) || 'N/A'}`);
console.log(`Tags: ${span.tags.join(', ') || 'none'}`);
console.log(`User ID: ${span.userId || 'N/A'}`);
console.log(`Session ID: ${span.sessionId || 'N/A'}`);
console.log(`Created: ${span.createdAt}`);
Error Handling
Copy
import { TraciaError, TraciaErrorCode } from 'tracia';
try {
const span = await tracia.spans.get('sp_abc123def456');
} catch (error) {
if (error instanceof TraciaError) {
if (error.code === TraciaErrorCode.NOT_FOUND) {
console.error('Span does not exist');
}
}
}

