Class SamplingLLMCallPublisher

java.lang.Object
com.tnsai.quality.sampling.SamplingLLMCallPublisher
All Implemented Interfaces:
com.tnsai.llm.observability.LLMCallPublisher

public final class SamplingLLMCallPublisher extends Object implements com.tnsai.llm.observability.LLMCallPublisher
LLMCallPublisher decorator that consults an EventSamplingPolicy before delegating each captured call (TNS-417, follow-up to TNS-374).

Mirrors SamplingAgentEventPublisher for the LLMCallPublisher SPI: keeps the SPI in tnsai-llm unchanged, lets consumers opt in by wrapping. Reuses the existing sampling policy machinery (head-based, rate-limited, error-always, priority-based, …) — the bridge is constructed by mapping each LLMCallLog into a SamplingInput:

  • eventKind = "llm.called"
  • level = EventLevel.ERROR when LLMCallLog.isFailure(), otherwise EventLevel.INFO. This is what makes ErrorAlwaysPolicy pass every failed call regardless of the tenant's nominal sample rate.
  • context = LLMCallLog.context() (tenant, agent, capability, …) so tenant-aware policies can read tenant id.

Composition:


 LLMCallPublisher otlp = ...;
 LLMCallPublisher chain = new SamplingLLMCallPublisher(
         new RedactingLLMCallPublisher(otlp, redactor),
         SamplingPolicies.production());
 
Order matters: redaction inside, sampling outside, so calls that survive sampling are already PII-safe and the redactor only pays the cost of the events that actually ship downstream.

Thread-safety: stateless beyond the immutable delegate + policy. Policies are required by their SPI to be thread-safe.

Since:
0.10.1 (TNS-417)
  • Field Details

    • EVENT_KIND

      public static final String EVENT_KIND
      Stable SamplingInput.eventKind() value used for every captured LLM call. Lets rule-based policies key off "llm.called" the same way they key off "tool.invoked" or "chat.chunk" for AgentEventPublisher.
      See Also:
  • Constructor Details

    • SamplingLLMCallPublisher

      public SamplingLLMCallPublisher(com.tnsai.llm.observability.LLMCallPublisher delegate, EventSamplingPolicy policy)
  • Method Details

    • publish

      public void publish(com.tnsai.llm.observability.LLMCallLog call)
      Specified by:
      publish in interface com.tnsai.llm.observability.LLMCallPublisher