OpenInference JS
    Preparing search index...
    • Experimental

      Wraps a function with tracing capabilities, specifically marking it as a TOOL span.

      This is a convenience function that wraps withSpan with the OpenInference span kind pre-configured to TOOL. Tool spans represent external tools, utilities, or services that an LLM application can invoke, such as APIs, databases, calculators, web scrapers, or any external function that provides specific capabilities.

      This API is experimental and may change in future versions

      Type Parameters

      • Fn extends AnyFn

        The function type being wrapped, preserving original signature

      Parameters

      • fn: Fn

        The function to wrap with TOOL span tracing

      • Optionaloptions: Omit<SpanTraceOptions<AnyFn>, "kind">

        Configuration options for tracing behavior (excluding kind)

        • tracer

          Custom OpenTelemetry tracer instance (defaults to global tracer)

        • name

          Custom span name (defaults to function name)

        • openTelemetrySpanKind

          OpenTelemetry span kind (defaults to INTERNAL)

        • processInput

          Custom function to process input arguments into attributes

        • processOutput

          Custom function to process output values into attributes

        • attributes

          Base attributes to be added to every span created

      Returns Fn

      A wrapped function with identical signature that creates TOOL spans during execution

      // Trace an API call tool
      const fetchWeather = async (city: string) => {
      const response = await fetch(`/api/weather?city=${city}`);
      return response.json();
      };
      const tracedWeatherTool = traceTool(fetchWeather, { name: "weather-api" });

      // Trace a calculator tool
      const calculate = (expression: string) => {
      return eval(expression); // Note: eval is dangerous, use proper parser
      };
      const tracedCalculator = withToolSpan(calculate, { name: "calculator" });

      // Trace a database query tool
      const queryDatabase = async (query: string, params: any[]) => {
      return await db.query(query, params);
      };
      const tracedDbTool = traceTool(queryDatabase);