JS/TS SDK
The JS/TS SDK is a set of npm packages built on OpenTelemetry. Spans are
exported to https://<your-ants-host>/api/public/otel/v1/traces and mapped to
traces and observations.
| Package | Purpose | Runtime |
|---|---|---|
@antsplatform/tracing | startActiveObservation, startObservation, observe, updateActiveTrace | Node.js 20+ |
@antsplatform/otel | AntsPlatformSpanProcessor, which exports spans to ANTS Platform | Node.js 20+ |
@antsplatform/openai | observeOpenAI wrapper for the OpenAI SDK | Any JS runtime |
@antsplatform/langchain | CallbackHandler for LangChain.js | Any JS runtime |
@antsplatform/client | AntsPlatformClient for prompts, datasets, scores and the REST API | Any JS runtime |
@antsplatform/guardrails | Guardrail checks and provider wrappers | Node.js |
ants-platform | Re-exports all of the above | Node.js 20+ |
The current version of every package is 1.0.16.
Set up tracing
Install
Set credentials
Create a key pair under Project Settings > API Keys (see API Keys):
The JS/TS SDK reads ANTS_PLATFORM_BASE_URL, not ANTS_PLATFORM_HOST (the
Python SDK variable). If neither the variable nor baseUrl is set, it sends
to https://api.agenticants.ai.
Register the span processor at startup
Create instrumentation.ts and import it before the rest of your app:
The agent option tags every span with a stable agent ID so the agent appears
in the AI Agent Center. Keep agentName stable. The display name can
change later.
Trace an agent
startActiveObservation makes the observation active, so observations created
inside it nest under it. It ends the observation when the function returns or
its promise settles. startObservation returns an observation that you end
with .end().
Cost is calculated by ANTS Platform from the model and token usage. Pass
costDetails to send your own cost instead.
Wrap a function with observe
Options: name, asType, captureInput, captureOutput, parentSpanContext
and endOnExit.
Observation types and attributes
asType accepts span (default), generation, event, agent, tool,
chain, retriever, evaluator, guardrail and embedding.
| Attributes | Fields |
|---|---|
| All observations | input, output, metadata, level, statusMessage, version, environment |
| Generations and embeddings | model, modelParameters, usageDetails, costDetails, completionStartTime, prompt |
Trace (updateActiveTrace, observation.updateTrace) | name, userId, sessionId, version, release, input, output, metadata, tags, public, environment |
Flush in short-lived processes
Spans are batched. In scripts and serverless functions, flush before the process ends:
Or set exportMode: "immediate" on the span processor to export each span as
soon as it ends.
Span processor options
| Option | Environment variable | Default |
|---|---|---|
publicKey | ANTS_PLATFORM_PUBLIC_KEY | none |
secretKey | ANTS_PLATFORM_SECRET_KEY | none |
baseUrl | ANTS_PLATFORM_BASE_URL | https://api.agenticants.ai |
environment | ANTS_PLATFORM_TRACING_ENVIRONMENT | none |
release | ANTS_PLATFORM_RELEASE | none |
flushAt | ANTS_PLATFORM_FLUSH_AT | OpenTelemetry batch default |
flushInterval (seconds) | ANTS_PLATFORM_FLUSH_INTERVAL | OpenTelemetry batch default |
timeout (seconds) | ANTS_PLATFORM_TIMEOUT | 5 |
exportMode | none | "batched"; "immediate" for serverless |
agent | none | { agentName, agentDisplayName } |
mask | none | Function applied to data before export |
shouldExportSpan | none | Function that filters spans |
additionalHeaders | none | Extra HTTP headers |
Framework integrations
Already using another OpenTelemetry instrumentation? Add
AntsPlatformSpanProcessor to the same tracer provider, or export OTLP
directly. See OpenTelemetry (OTLP).
API client
AntsPlatformClient from @antsplatform/client covers prompts, datasets,
scores and read access to traces, observations and sessions. It reads the same
environment variables (ANTS_PLATFORM_BASE_URL defaults to
https://api.agenticants.ai; ANTS_PLATFORM_TIMEOUT defaults to 30 seconds).
Members include prompt, dataset, score, media, api (the full REST
client), getPrompt, createPrompt, getDataset, fetchTrace,
fetchTraces, fetchObservations, fetchSessions, getTraceUrl,
updateAgentDisplayName, flush and shutdown. AntsPlatformClient does not
create traces. Use @antsplatform/tracing for that.
Troubleshooting
- No traces. Check that
instrumentation.tsruns before your app code, that the keys andANTS_PLATFORM_BASE_URLare set, and that the process flushes before it exits. 401on export. Both keys must belong to the same project.- Agent missing from the AI Agent Center. Set the
agentoption onAntsPlatformSpanProcessor.