If your preferred programming language or platform is not directly supported by the existing LangWatch libraries, you can use the REST API with curl to send trace data. This guide will walk you through how to integrate LangWatch with any system that allows HTTP requests.

Prerequisites:

  • Ensure you have curl installed on your system.

Configuration:

Set the LANGWATCH_API_KEY environment variable in your environment:

export LANGWATCH_API_KEY='your_api_key_here'

Usage:

You will need to prepare your span data in accordance with the Span type definitions provided by LangWatch. Below is an example of how to send span data using curl:

  1. Prepare your JSON data. Make sure it’s properly formatted as expected by LangWatch.
  2. Use the curl command to send your trace data. Here is a basic template:
# Set your API key and endpoint URL
API_KEY="your_langwatch_api_key"
ENDPOINT="https://app.langwatch.ai/api/collector"

# Use curl to send the POST request, e.g.:
curl -X POST "$ENDPOINT" \
     -H "X-Auth-Token: $API_KEY" \
     -H "Content-Type: application/json" \
     -d @- <<EOF
{
  "trace_id": "trace-123",
  "spans": [
    {
      "type": "llm",
      "span_id": "span-456",
      "vendor": "openai",
      "model": "gpt-4",
      "input": {
        "type": "chat_messages",
        "value": [
          {
            "role": "user",
            "content": "Input to the LLM"
          }
        ]
      },
      "output": {
        "type": "chat_messages",
        "value": [
            {
                "role": "assistant",
                "content": "Output from the LLM",
                "function_call": null,
                "tool_calls": []
            }
        ]
      },
      "params": {
        "temperature": 0.7,
        "stream": false
      },
      "metrics": {
        "prompt_tokens": 100,
        "completion_tokens": 150
      },
      "timestamps": {
        "started_at": 1706628806000,
        "finished_at": 1706628808000
      }
    }
  ],
  "metadata": {
    "user_id": "optional_end_user_identifier",
    "thread_id": "optional_thread_identifier",
    "customer_id": "optional_platform_customer_identifier",
    "labels": ["optional_label_1", "optional_label_2"]
  }
}
EOF

Replace the placeholders with your actual data. The @- tells curl to read the JSON data from the standard input, which we provide via the EOF-delimited here-document.

For the type reference of how a span should look like, check out our types definitions.

It’s optional but highly recommended to pass the user_id on the metadata if you want to leverage user-specific analytics and the thread_id to group related traces together. To connect it to an event later on. Read more about those and other concepts here.

  1. Execute the curl command. If successful, LangWatch will process your trace data.

This method of integration offers a flexible approach for sending traces from any system capable of making HTTP requests. Whether you’re using a less common programming language or a custom-built platform, this RESTful approach ensures you can benefit from LangWatch’s capabilities.

Remember to handle errors and retries as needed. You might need to script additional logic around the curl command to handle these cases.

After following the above guide, your interactions with LLMs should now be captured by LangWatch. Once integrated, you can visit your LangWatch dashboard to view and analyze the traces collected from your applications.