1bun add @halfspan/sdk
123456789101112131415import { agentTraceBatch, type AgentTraceEvent } from '@halfspan/sdk'; const run = { session: crypto.randomUUID(), segment: 'managed' as const, task: 'task-id', attempt: 'a1', }; const emit = (event: AgentTraceEvent) => process.stdout.write(JSON.stringify(agentTraceBatch(event)) + '\n'); emit({ ...run, id: crypto.randomUUID(), at: new Date(), kind: 'application_log', code: 'agent.task.started', severity: 'info', });
info, warning, or error. It does not forward a freeform log message. Write other application output to stderr to keep stdout suitable for the sidecar.12span login bun run agent.ts | span hook ingest
span auth status to check the connection.createAgentTraceHook({ apiUrl, ingestToken }) with a workspace service credential in the server environment. The same events can be passed to that callback instead of writing NDJSON. See the CLI reference for credential configuration and SDK harness integration for direct HTTP ingestion.| Signal | Event |
| Tool Failures | tool_receipt with state: 'failed' |
| Application Errors | application_log with severity: 'error' |
| Failed Outcomes | outcome with result: 'failed' |
| Retries | retry |
| Human Approvals | approval_waiting |
| Reported Outcomes | outcome with required_checks_passed for coding agents or customer_resolved for managed agents |
emit({ ...run, id: crypto.randomUUID(), at: new Date(), kind: 'tool_receipt', operation: 'lookup', reference: 'call-id', state: 'failed', }); emit({ ...run, id: crypto.randomUUID(), at: new Date(), kind: 'outcome', result: 'customer_resolved', receipt: 'resolution-receipt-id', }); emit({ ...run, id: crypto.randomUUID(), at: new Date(), kind: 'retry', previousAttempt: 'a0', }); emit({ ...run, id: crypto.randomUUID(), at: new Date(), kind: 'approval_waiting', });
required_checks_passed with segment: 'coding', customer_resolved with segment: 'managed', and deliverable_accepted with segment: 'business'. A failed result is valid in every segment. A signal match counts an event, not a unique issue or user.inputTokens and outputTokens on an SDK usage event. When the provider supplies a cost, include usdMicros in millionths of a US dollar. For a Vercel AI SDK integration, costMicrosForStep(step) can return the reported cost for that step. Omit the cost when it is unavailable. Token counts still appear, and the dashboard shows how many records contributed to a partial spend total.provider_reported preserves the supplied halfspan.cost.usd_micros. estimated_catalog applies the recorded catalog version to text input, cached input, and output tokens with a single rounding step to microdollars. unknown leaves spend empty. The 2026-11-29 catalog covers the published standard text-token rates for GPT-4o mini, GPT-4.1 mini, GPT-4.1, and GPT-5 mini.query event for each user intent and one response event for each assessed reply. Their wire query and response identifiers equal their event IDs. Link follow-up queries with parentQueryId only when they continue the same task. Record the prompt version used at execution time; changing a template later does not change past exposures.await onTrace({ ...run, id: 'query-1', at: new Date(), kind: 'query', parentQueryId: null, }); await onTrace({ ...run, id: 'exposure-1', at: new Date(), kind: 'prompt_exposure', queryId: 'query-1', promptId: 'support-answer', promptVersion: 'v2', }); await onTrace({ ...run, id: 'response-1', at: new Date(), kind: 'response', queryId: 'query-1', }); await onTrace({ ...run, id: 'feedback-1', at: new Date(), kind: 'user_feedback', targetEventId: 'response-1', dimension: 'helpfulness', value: 'negative', reasonCode: 'instruction_missed', });
helpfulness, correctness, and intent_match, with positive, negative, or neutral values. Inferred behavior uses a versioned behavior_signal with an evidence reference and positive, negative, neutral, or unknown assessment. Keep those sources separate from confirmed task outcomes. Silence is not positive feedback, a repeated query may be a new task, and a safe refusal may be the correct result.builtInSignalDefinitions, evaluateSignalsEffect, and signalEvaluationEvents. Presets describe satisfaction, frustration, corrections, instruction adherence, premature completion, and recovery. Supply your evaluator and provider configuration; Halfspan does not infer satisfaction from keywords or missing feedback.evaluateSignalsEffect accepts explicit content access, evidence with original event IDs, an evaluator version, concurrency of 1–4, and a timeout up to 30 seconds. The evaluator receives an abort signal and must return an assessment, explanation, and references drawn from the supplied evidence. Unsupported content and failed evaluations return an unavailable result. Your runtime owns durable scheduling, retries, provider spend limits, and the decision to share content.signalEvaluationEvents({ context, evaluation, idForEvidence }) and pass its events to your trace hook. Supply stable IDs per definition, definition version, evaluator version, subject, and evidence reference; reuse the original observation time on replay. Conversion retains evaluator versions, evaluation status, reason codes, and all evidence references in one assessment event. Unavailable and not-applicable results keep an unknown assessment with distinct statuses; they do not count as assessed targets.TYPESAFE_API_KEY and HALFSPAN_TRACE_LABEL_MODEL on the API server, then restart it. Choose a versioned Jev model ID from your TypeSafe account. Apply the telemetry migration with just db-migrate and grant the runtime role SELECT, INSERT, DELETE on agent_trace_labels (infra/observability/local-role.sql includes the local grant).triage-v1 taxonomy asks independently about tool failures, provider failures, user corrections, repeated attempts, incomplete work, and recovery. The trace displays labels with probabilities at or above 0.7 and retains every probability under Label details. This display threshold has not been calibrated against your traffic. No label above the threshold does not establish success or absence of a problem.