Class CurrentInvocation

java.lang.Object
dev.langchain4j.internal.CurrentInvocation

public final class CurrentInvocation extends Object
Holds the InvocationContext.invocationId() of the AI Service invocation that is doing its own work on the current thread (for example running a tool, a guardrail, retrieval or the chatRequestTransformer), so that an AI Service invoked by that work can record it as its parent invocation.

It is set only around that work, not around code of the caller (the handlers of a TokenStream, the subscriber of a returned publisher, the continuations of a returned future), so an AI Service invoked by the caller's code normally has no parent invocation.

  • Method Details

    • invocationId

      public static UUID invocationId()
    • callWith

      public static <T> T callWith(InvocationContext invocationContext, Supplier<T> action)
      Calls the given action with the id of the given invocation context as the current one, then restores the previous one. Does nothing special when the invocation context is null.
    • callWithout

      public static <T> T callWithout(Supplier<T> action)
      Calls the given action with no current invocation, then restores the previous one. Used when an AI Service is invoked during the work of another invocation: its listeners and callbacks are not part of that work.