Track Usage API

This is the bread and butter API that you'll use to track usage

🎉If you're using one of our OpenAI or Azure OpenAI wrappers from the quickstart, this API is automatically implemented for you! No need to manually implement this

Note: One API request is one "message". What does that mean? It means if you send 3 trackUsage API requests for companyA, we'll count that as 3 different messages for companyA. So if you bill "per message", you'll bill them for 3 messages

Some key concepts to know:

  • key (required) - This key is what your usage will be stored under. For B2B use cases, this is usually a customer name.

    • As an example, if your customer, companyA has used 10 tokens, you would call the trackUsage API with company as the key

  • query (optional) - you can track the query you input into the LLM

  • tags (optional) - you can add tags to usage for easy searching/filtering. In JSON array format (["test", "test2"])

    • For example, if you have a classification prompt, you can add the classification prompt tag. And if you have a response prompt, you can add the response prompt tag. Then you can easily filter!

  • conversationId (optional) - If your product has conversations, you can use this to relate many requests to 1 conversation

    • For example, if you bill "$1 per conversation", a customer can have 6 messages/requests in one conversation. You would then call trackUsage with those 6 requests and set the conversationId to the same thing.

  • openAiResponse (optional) - If you use OpenAI, you can easily just pass in the completion/chat/embedding response here. We'll automatically handle it

  • usage (optional) - If you don't use OpenAI, you'll want to use this to track token usage.

    • The general format looks like this

"usage": {​
    "prompt_tokens": 0,​
    "completion_tokens": 0,​
    "total_tokens": 0​
},

Some examples in Javascript:

// For OpenAI
const trackUsageResponse = await spryngtimeanalyticssdk.usageTracking.trackUsage({
            key: "company123",
            query: "what is 2+123123",
            openAiResponse: chatCompletion,
            conversationId: "test123",
            tags: ["test", "test2"],
        });

// Non OpenAI
const trackUsageResponse = await spryngtimeanalyticssdk.usageTracking.trackUsage({
            key: "company123",
            usage: {​
                "prompt_tokens": 0,
                "completion_tokens": 0,
                "total_tokens": 0
            },
            conversationId: "456",
        });

Some examples in Python:

# For OpenAi
spryngtimeanalyticssdk.usage_tracking.track_usage(
        key="company5",
        conversation_id="convo1",
        open_ai_response=chatCompletion)
        
# For Non OpenAI
 spryngtimeanalyticssdk.usage_tracking.track_usage(
        key="company5",
        conversation_id="convo1",
        usage={
            "prompt_tokens": 1,
            "completion_tokens": 1,
            "total_tokens": 1,
        },
    )

Last updated