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, provider wrappers and tool governance | Node.js |
ants-platform | Re-exports all of the above | Node.js 20+ |
The current version of every package is 1.0.17.
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).
Tool governance
From 1.0.17, AntsGuardrailsClient in @antsplatform/guardrails checks an
agent's tool calls against its tool permissions
before the tool runs. It is on by default when agentId is set, and changes
nothing until the agent has an approved tool.
guardToolreads the function's first argument as the tool's arguments, checks the call before the function runs, and records it as a tool observation when@antsplatform/tracingis installed. The returned function is async.- When the agent's AI Guardrails policy is Block or Redact, a call that breaks
a rule throws
ToolNotAllowedError. Under Audit Only, or with no policy, the call runs and the violation is reported in the background. - A call that needs approval creates an approval request and waits for the
decision. A denial, an expiry or no answer within
approvalTimeoutMsthrowsToolApprovalDeniedError. - Tool calls of a suspended
agent throw
ToolNotAllowedError, even when the agent has no policy.
| Helper | Use |
|---|---|
guardTool(fn, { name }) | Wraps a tool function |
guardFrameworkTool(guardrails, tool) / guardFrameworkTools(guardrails, tools) | Guards LangChain tools and plain functions in place |
langchainToolGuard(guardrails) | A LangChain callback handler that checks every tool call before it starts |
guardMcpClient(guardrails, client, "github") | Checks MCP callTool as github/<tool>, and compares listTools definitions with the pinned ones |
runAs(idToken, fn) / setUserToken(idToken) | Verifies the OIDC ID token of the user the agent acts for, for group-limited tools |
checkToolCalls(...) / enforceToolCalls(...) | Check calls yourself. checkToolCalls never throws. |
The provider wrappers also check the tool calls in each model response.
| Option | Default | Description |
|---|---|---|
enforceTools | true | Pass false to turn tool governance off |
toolFailMode | "open" | "closed" blocks tool calls when the tool settings were never loaded |
approvalTimeoutMs | 300000 | How long to wait for an approval |
approvalPollMs | 2000 | How often to ask for the decision |
The tool settings are refreshed about every minute, and the last copy is kept
if a refresh fails. Rate limits are counted in the process. The JS guardrail
checks of prompts and responses do not ask for the WARNING result, so a
check that warns returns PASS; see
Who receives the Warning result.
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.