Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
Add Support for LangChain
patcher979434f2e7370
diff --git a/sdk/python/src/openlit/instrumentation/langchain/__init__.py b/sdk/python/src/openlit/instrumentation/langchain/__init__.pyindex 805138ee..2d5037a1 100644--- a/sdk/python/src/openlit/instrumentation/langchain/__init__.py+++ b/sdk/python/src/openlit/instrumentation/langchain/__init__.py@@ -5,7 +5,14 @@ import importlib.metadatafrom opentelemetry.instrumentation.instrumentor import BaseInstrumentorfrom wrapt import wrap_function_wrapper-from openlit.instrumentation.langchain.langchain import general_wrap, hub, llm, allm+from openlit.instrumentation.langchain.langchain import (+ general_wrap,+ hub,+ llm,+ allm,+ chat,+ achat+)_instruments = ("langchain >= 0.1.20",)@@ -52,6 +59,18 @@ WRAPPED_METHODS = ["endpoint": "langchain.llm","wrapper": allm,},+ {+ "package": "langchain_core.language_models.chat_models",+ "object": "BaseChatModel.invoke",+ "endpoint": "langchain.chat_models",+ "wrapper": chat,+ },+ {+ "package": "langchain_core.language_models.chat_models",+ "object": "BaseChatModel.ainvoke",+ "endpoint": "langchain.chat_models",+ "wrapper": achat,+ },]class LangChainInstrumentor(BaseInstrumentor):@@ -66,6 +85,8 @@ class LangChainInstrumentor(BaseInstrumentor):tracer = kwargs.get("tracer")pricing_info = kwargs.get("pricing_info")trace_content = kwargs.get("trace_content")+ metrics = kwargs.get("metrics_dict")+ disable_metrics = kwargs.get("disable_metrics")version = importlib.metadata.version("langchain")for wrapped_method in WRAPPED_METHODS:@@ -77,7 +98,7 @@ class LangChainInstrumentor(BaseInstrumentor):wrap_package,wrap_object,wrapper(gen_ai_endpoint, version, environment, application_name,- tracer, pricing_info, trace_content),+ tracer, pricing_info, trace_content, metrics, disable_metrics),)@staticmethoddiff --git a/sdk/python/src/openlit/instrumentation/langchain/langchain.py b/sdk/python/src/openlit/instrumentation/langchain/langchain.pyindex f7b3fbce..d9613288 100644--- a/sdk/python/src/openlit/instrumentation/langchain/langchain.py+++ b/sdk/python/src/openlit/instrumentation/langchain/langchain.py@@ -6,14 +6,14 @@ Module for monitoring Langchain applications.import loggingfrom opentelemetry.trace import SpanKind, Status, StatusCodefrom opentelemetry.sdk.resources import TELEMETRY_SDK_NAME-from openlit.__helpers import handle_exception+from openlit.__helpers import handle_exception, get_chat_model_cost, general_tokensfrom openlit.semcov import SemanticConvetion# Initialize logger for logging potential issues and operationslogger = logging.getLogger(__name__)def general_wrap(gen_ai_endpoint, version, environment, application_name,- tracer, pricing_info, trace_content):+ tracer, pricing_info, trace_content, metrics, disable_metrics):"""Creates a wrapper around a function call to trace and log its execution metrics.@@ -86,7 +86,7 @@ def general_wrap(gen_ai_endpoint, version, environment, application_name,return wrapperdef hub(gen_ai_endpoint, version, environment, application_name, tracer,- pricing_info, trace_content):+ pricing_info, trace_content, metrics, disable_metrics):"""Creates a wrapper around Langchain hub operations for tracing and logging.@@ -162,7 +162,7 @@ def hub(gen_ai_endpoint, version, environment, application_name, tracer,def allm(gen_ai_endpoint, version, environment, application_name,- tracer, pricing_info, trace_content):+ tracer, pricing_info, trace_content, metrics, disable_metrics):"""Creates a wrapper around a function call to trace and log its execution metrics.@@ -207,6 +207,16 @@ def allm(gen_ai_endpoint, version, environment, application_name,response = await wrapped(*args, **kwargs)try:+ prompt = args[0] or ""+ # input_tokens = general_tokens(prompt)+ # output_tokens = general_tokens(response)++ # # Calculate cost of the operation+ # cost = get_chat_model_cost(+ # str(getattr(instance, 'model')),+ # pricing_info, input_tokens, output_tokens+ # )+span.set_attribute(TELEMETRY_SDK_NAME, "openlit")span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN)@@ -226,13 +236,56 @@ def allm(gen_ai_endpoint, version, environment, application_name,str(getattr(instance, 'top_k')))span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,str(getattr(instance, 'top_p')))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,+ False)+ # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,+ # input_tokens)+ # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,+ # output_tokens)+ # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,+ # input_tokens + output_tokens)+ # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,+ # cost)if trace_content:- span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,- args[0])- span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,- response)+ span.add_event(+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,+ attributes={+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,+ },+ )+ span.add_event(+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,+ attributes={+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response,+ },+ )+span.set_status(Status(StatusCode.OK))+ # if disable_metrics is False:+ # attributes = {+ # TELEMETRY_SDK_NAME:+ # "openlit",+ # SemanticConvetion.GEN_AI_APPLICATION_NAME:+ # application_name,+ # SemanticConvetion.GEN_AI_SYSTEM:+ # SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN,+ # SemanticConvetion.GEN_AI_ENVIRONMENT:+ # environment,+ # SemanticConvetion.GEN_AI_TYPE:+ # SemanticConvetion.GEN_AI_TYPE_CHAT,+ # SemanticConvetion.GEN_AI_REQUEST_MODEL:+ # str(getattr(instance, 'model'))+ # }++ # metrics["genai_requests"].add(1, attributes)+ # metrics["genai_total_tokens"].add(+ # input_tokens + output_tokens, attributes+ # )+ # metrics["genai_completion_tokens"].add(output_tokens, attributes)+ # metrics["genai_prompt_tokens"].add(input_tokens, attributes)+ # metrics["genai_cost"].record(cost, attributes)+# Return original responsereturn response@@ -246,7 +299,7 @@ def allm(gen_ai_endpoint, version, environment, application_name,return wrapperdef llm(gen_ai_endpoint, version, environment, application_name,- tracer, pricing_info, trace_content):+ tracer, pricing_info, trace_content, metrics, disable_metrics):"""Creates a wrapper around a function call to trace and log its execution metrics.@@ -291,6 +344,16 @@ def llm(gen_ai_endpoint, version, environment, application_name,response = wrapped(*args, **kwargs)try:+ prompt = args[0] or ""+ # input_tokens = general_tokens(prompt)+ # output_tokens = general_tokens(response)++ # # Calculate cost of the operation+ # cost = get_chat_model_cost(+ # str(getattr(instance, 'model')),+ # pricing_info, input_tokens, output_tokens+ # )+span.set_attribute(TELEMETRY_SDK_NAME, "openlit")span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN)@@ -310,13 +373,56 @@ def llm(gen_ai_endpoint, version, environment, application_name,str(getattr(instance, 'top_k')))span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,str(getattr(instance, 'top_p')))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,+ False)+ # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,+ # input_tokens)+ # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,+ # output_tokens)+ # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,+ # input_tokens + output_tokens)+ # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,+ # cost)if trace_content:- span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,- args[0])- span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,- response)+ span.add_event(+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,+ attributes={+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,+ },+ )+ span.add_event(+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,+ attributes={+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response,+ },+ )+span.set_status(Status(StatusCode.OK))+ # if disable_metrics is False:+ # attributes = {+ # TELEMETRY_SDK_NAME:+ # "openlit",+ # SemanticConvetion.GEN_AI_APPLICATION_NAME:+ # application_name,+ # SemanticConvetion.GEN_AI_SYSTEM:+ # SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN,+ # SemanticConvetion.GEN_AI_ENVIRONMENT:+ # environment,+ # SemanticConvetion.GEN_AI_TYPE:+ # SemanticConvetion.GEN_AI_TYPE_CHAT,+ # SemanticConvetion.GEN_AI_REQUEST_MODEL:+ # str(getattr(instance, 'model'))+ # }++ # metrics["genai_requests"].add(1, attributes)+ # metrics["genai_total_tokens"].add(+ # input_tokens + output_tokens, attributes+ # )+ # metrics["genai_completion_tokens"].add(output_tokens, attributes)+ # metrics["genai_prompt_tokens"].add(input_tokens, attributes)+ # metrics["genai_cost"].record(cost, attributes)+# Return original responsereturn response@@ -328,3 +434,279 @@ def llm(gen_ai_endpoint, version, environment, application_name,return responsereturn wrapper++def chat(gen_ai_endpoint, version, environment, application_name,+ tracer, pricing_info, trace_content, metrics, disable_metrics):+ """+ Creates a wrapper around a function call to trace and log its execution metrics.++ This function wraps any given function to measure its execution time,+ log its operation, and trace its execution using OpenTelemetry.++ Parameters:+ - gen_ai_endpoint (str): A descriptor or name for the endpoint being traced.+ - version (str): The version of the Langchain application.+ - environment (str): The deployment environment (e.g., 'production', 'development').+ - application_name (str): Name of the Langchain application.+ - tracer (opentelemetry.trace.Tracer): The tracer object used for OpenTelemetry tracing.+ - pricing_info (dict): Information about the pricing for internal metrics (currently not used).+ - trace_content (bool): Flag indicating whether to trace the content of the response.++ Returns:+ - function: A higher-order function that takes a function 'wrapped' and returns+ a new function that wraps 'wrapped' with additional tracing and logging.+ """++ def wrapper(wrapped, instance, args, kwargs):+ """+ An inner wrapper function that executes the wrapped function, measures execution+ time, and records trace data using OpenTelemetry.++ Parameters:+ - wrapped (Callable): The original function that this wrapper will execute.+ - instance (object): The instance to which the wrapped function belongs. This+ is used for instance methods. For static and classmethods,+ this may be None.+ - args (tuple): Positional arguments passed to the wrapped function.+ - kwargs (dict): Keyword arguments passed to the wrapped function.++ Returns:+ - The result of the wrapped function call.++ The wrapper initiates a span with the provided tracer, sets various attributes+ on the span based on the function's execution and response, and ensures+ errors are handled and logged appropriately.+ """+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:+ response = wrapped(*args, **kwargs)++ try:+ input_tokens = response.response_metadata["prompt_eval_count"] or 0+ output_tokens = response.response_metadata["eval_count"] or 0++ # Calculate cost of the operation+ cost = get_chat_model_cost(+ str(getattr(instance, 'model')),+ pricing_info, input_tokens, output_tokens+ )++ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,+ SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN)+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,+ gen_ai_endpoint)+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,+ environment)+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,+ SemanticConvetion.GEN_AI_TYPE_CHAT)+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,+ application_name)+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,+ str(getattr(instance, 'model')))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,+ str(getattr(instance, 'temperature')))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_K,+ str(getattr(instance, 'top_k')))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,+ str(getattr(instance, 'top_p')))+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,+ [response.response_metadata["done_reason"]])+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,+ False)+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,+ input_tokens)+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,+ output_tokens)+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,+ input_tokens + output_tokens)+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,+ cost)+ if trace_content:+ span.add_event(+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,+ attributes={+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: args[0],+ },+ )+ span.add_event(+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,+ attributes={+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.content,+ },+ )++ span.set_status(Status(StatusCode.OK))++ if disable_metrics is False:+ attributes = {+ TELEMETRY_SDK_NAME:+ "openlit",+ SemanticConvetion.GEN_AI_APPLICATION_NAME:+ application_name,+ SemanticConvetion.GEN_AI_SYSTEM:+ SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN,+ SemanticConvetion.GEN_AI_ENVIRONMENT:+ environment,+ SemanticConvetion.GEN_AI_TYPE:+ SemanticConvetion.GEN_AI_TYPE_CHAT,+ SemanticConvetion.GEN_AI_REQUEST_MODEL:+ str(getattr(instance, 'model'))+ }++ metrics["genai_requests"].add(1, attributes)+ metrics["genai_total_tokens"].add(+ input_tokens + output_tokens, attributes+ )+ metrics["genai_completion_tokens"].add(output_tokens, attributes)+ metrics["genai_prompt_tokens"].add(input_tokens, attributes)+ metrics["genai_cost"].record(cost, attributes)++ # Return original response+ return response++ except Exception as e:+ handle_exception(span, e)+ logger.error("Error in trace creation: %s", e)++ # Return original response+ return response++ return wrapper++def achat(gen_ai_endpoint, version, environment, application_name,+ tracer, pricing_info, trace_content, metrics, disable_metrics):+ """+ Creates a wrapper around a function call to trace and log its execution metrics.++ This function wraps any given function to measure its execution time,+ log its operation, and trace its execution using OpenTelemetry.++ Parameters:+ - gen_ai_endpoint (str): A descriptor or name for the endpoint being traced.+ - version (str): The version of the Langchain application.+ - environment (str): The deployment environment (e.g., 'production', 'development').+ - application_name (str): Name of the Langchain application.+ - tracer (opentelemetry.trace.Tracer): The tracer object used for OpenTelemetry tracing.+ - pricing_info (dict): Information about the pricing for internal metrics (currently not used).+ - trace_content (bool): Flag indicating whether to trace the content of the response.++ Returns:+ - function: A higher-order function that takes a function 'wrapped' and returns+ a new function that wraps 'wrapped' with additional tracing and logging.+ """++ async def wrapper(wrapped, instance, args, kwargs):+ """+ An inner wrapper function that executes the wrapped function, measures execution+ time, and records trace data using OpenTelemetry.++ Parameters:+ - wrapped (Callable): The original function that this wrapper will execute.+ - instance (object): The instance to which the wrapped function belongs. This+ is used for instance methods. For static and classmethods,+ this may be None.+ - args (tuple): Positional arguments passed to the wrapped function.+ - kwargs (dict): Keyword arguments passed to the wrapped function.++ Returns:+ - The result of the wrapped function call.++ The wrapper initiates a span with the provided tracer, sets various attributes+ on the span based on the function's execution and response, and ensures+ errors are handled and logged appropriately.+ """+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:+ response = await wrapped(*args, **kwargs)++ try:+ input_tokens = response.response_metadata["prompt_eval_count"] or 0+ output_tokens = response.response_metadata["eval_count"] or 0++ # Calculate cost of the operation+ cost = get_chat_model_cost(+ str(getattr(instance, 'model')),+ pricing_info, input_tokens, output_tokens+ )++ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,+ SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN)+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,+ gen_ai_endpoint)+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,+ environment)+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,+ SemanticConvetion.GEN_AI_TYPE_CHAT)+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,+ application_name)+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,+ str(getattr(instance, 'model')))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,+ str(getattr(instance, 'temperature')))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_K,+ str(getattr(instance, 'top_k')))+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,+ str(getattr(instance, 'top_p')))+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,+ [response.response_metadata["done_reason"]])+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,+ False)+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,+ input_tokens)+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,+ output_tokens)+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,+ input_tokens + output_tokens)+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,+ cost)+ if trace_content:+ span.add_event(+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,+ attributes={+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: args[0],+ },+ )+ span.add_event(+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,+ attributes={+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.content,+ },+ )++ span.set_status(Status(StatusCode.OK))++ if disable_metrics is False:+ attributes = {+ TELEMETRY_SDK_NAME:+ "openlit",+ SemanticConvetion.GEN_AI_APPLICATION_NAME:+ application_name,+ SemanticConvetion.GEN_AI_SYSTEM:+ SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN,+ SemanticConvetion.GEN_AI_ENVIRONMENT:+ environment,+ SemanticConvetion.GEN_AI_TYPE:+ SemanticConvetion.GEN_AI_TYPE_CHAT,+ SemanticConvetion.GEN_AI_REQUEST_MODEL:+ str(getattr(instance, 'model'))+ }++ metrics["genai_requests"].add(1, attributes)+ metrics["genai_total_tokens"].add(+ input_tokens + output_tokens, attributes+ )+ metrics["genai_completion_tokens"].add(output_tokens, attributes)+ metrics["genai_prompt_tokens"].add(input_tokens, attributes)+ metrics["genai_cost"].record(cost, attributes)++ # Return original response+ return response++ except Exception as e:+ handle_exception(span, e)+ logger.error("Error in trace creation: %s", e)++ # Return original response+ return response++ return wrapper\ No newline at end of file