Join our Newsletter — 33% off our NHI Course

How should teams instrument AI applications across different programming languages without losing visibility into token usage and cost?

Teams should use language native SDKs that capture AI specific signals at the client layer and export them through OpenTelemetry. That gives consistent traces, inputs, outputs, latency, token usage, and cost data across Java, Go, Ruby, and C#. The main goal is to avoid custom one off instrumentation and keep observability portable across backends and vendors.

Why This Matters for Security Teams

Cross-language AI instrumentation is not just an observability problem. It is a governance problem for usage, cost, and sensitive data handling. If teams cannot consistently capture prompt, completion, latency, token counts, and model context across Java, Go, Ruby, and C#, they lose the ability to compare workloads, detect abuse, and attribute spend to the right service. That creates blind spots that look small in one service and become expensive at scale.

The practical risk is that teams often instrument only the transport layer, then miss the AI-specific signals that explain why a workload became costly or risky. This is especially important when applications call external models, route through proxies, or move between vendors. Guidance from NIST SP 800-53 Rev 5 Security and Privacy Controls reinforces that monitoring must support accountability, while NHIMG research on Guide to the Secret Sprawl Challenge shows how quickly unmanaged telemetry and credentials can fragment across modern software stacks.

In practice, many security teams discover visibility gaps only after an AI workload has already exceeded budget, leaked sensitive context, or been routed through an unapproved model path.

How It Works in Practice

The most reliable pattern is to instrument at the language-native SDK layer, then export the same core signals through OpenTelemetry. That keeps application teams close to the code while still producing portable telemetry for downstream SIEM, APM, or cost analytics platforms. The SDK should capture request metadata, model name, token counts, latency, finish reason, errors, and where available, usage and billing fields returned by the provider.

This approach avoids brittle wrapper logic in each service. A Java service, a Go worker, a Ruby API, and a C# orchestration layer can all emit compatible spans if they follow the same semantic conventions. OpenTelemetry is the transport, not the policy engine, so teams still need to define what attributes are safe to record. For example, prompts may need redaction or truncation, while token totals and cost estimates can usually be preserved for financial and operational review. The key is consistency across codebases, not identical implementation code.

Teams should also separate observability from secrets handling. AI SDK instrumentation often touches API keys, project IDs, and routing headers, so the same controls that protect other application secrets should apply here. NHIMG’s reporting on The State of Secrets in AppSec highlights how remediation lag and developer behavior gaps can undermine even mature programs. In parallel, operational visibility should be validated against realistic breach patterns such as the Salesloft OAuth token breach, where token handling failures turned access into exposure.

  • Instrument the AI client, not just the HTTP layer, so token usage is captured where it is generated.
  • Export to OpenTelemetry with a shared schema for model, provider, latency, usage, and cost fields.
  • Redact prompts and outputs where content could contain secrets or regulated data.
  • Normalize metadata across languages so reporting does not depend on each team’s local conventions.
  • Validate that dashboards can reconcile usage to service, environment, and deployment version.

These controls tend to break down when teams rely on proxy-only logging for streaming responses, because usage details and final token totals are often unavailable until the client SDK completes the call.

Common Variations and Edge Cases

Tighter AI telemetry often increases privacy and engineering overhead, requiring organisations to balance cost visibility against data minimisation. That tradeoff is real, especially when prompts may include customer data, code, or internal context. Current guidance suggests capturing enough structure to support billing, incident response, and abuse detection without storing unnecessary content. There is no universal standard for how much prompt data should be retained yet.

Some environments need special handling. Batch jobs may aggregate usage after completion rather than emit per-call billing details. Streaming applications may only know final token counts at the end of the response. Multi-tenant platforms need tenant tags and request correlation so one model invocation can be attributed correctly. Where the provider does not expose cost directly, teams should estimate it from token counts and published pricing, then reconcile the estimate against billing exports.

Teams should also expect the hardest failures in polyglot estates with mixed instrumentation maturity. If one language uses a rich SDK wrapper and another emits only generic traces, the observability story becomes inconsistent fast. Best practice is evolving toward a shared AI telemetry contract across services, backed by policy checks in CI and runtime validation in production. Without that consistency, budget reports and security detections quickly diverge from actual usage.

NHIMG’s research on the Guide to the Secret Sprawl Challenge reinforces that fragmented tooling is where visibility breaks down first, and the same pattern applies to AI instrumentation when each team chooses a different logging shape.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10, OWASP Agentic AI Top 10 and CSA MAESTRO address the attack and risk surface, while NIST AI RMF and NIST CSF 2.0 set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP Non-Human Identity Top 10 NHI-05 Covers secure handling of credentials and telemetry in NHI-enabled apps.
OWASP Agentic AI Top 10 AI apps need runtime visibility into model activity and tool use.
CSA MAESTRO Agentic and AI workloads need consistent observability across services and vendors.
NIST AI RMF Supports governance of AI measurement, monitoring, and transparency.
NIST CSF 2.0 DE.CM-1 Continuous monitoring depends on consistent event capture and analysis.

Capture model calls, outputs, and usage at the client layer, then export through standard telemetry.