Integrating Signals with Cost Tracking
Overview
This guide shows you how to integrate cost tracking with signals in your AI agent workflow. By linking these two together, you can:
- Automatically attribute AI provider costs (OpenAI, Anthropic, etc.) to specific business events
- Track the true cost of achieving business outcomes (e.g., booking a meeting, sending an email, processing an image or a document)
- Generate accurate invoices that reflect both usage-based pricing and actual AI costs
Prerequisites
Before you begin, make sure you have:
- Created an API key in the Paid dashboard
- Installed the Paid SDK for your language
- Configured at least one Agent with signals
- Set up your customer and order records
Note: This feature is currently available in Python and Node.js SDKs only. Support for Go, Ruby, and Java is coming soon.
How It Works
In a typical AI agent workflow, your event processing logic might look like this:
To link AI costs with signals, you need to:
- Wrap your AI provider calls with Paid wrappers or hooks (e.g.,
PaidOpenAI
,PaidAnthropic
) - Wrap your entire event processing function in a tracing context (
Paid.trace()
or@paid_tracing()
) - Emit the signal within the same trace with
enable_cost_tracing=True
This ensures all AI calls and the signal share the same OpenTelemetry trace, allowing Paid to link costs to your business outcome.
Important: Cost Tracing Rules
Only enable cost tracing for ONE signal per trace
If you emit multiple signals within a single trace and enable cost tracing for more than one, the costs will be double-counted across multiple signals. The pattern should be:
- Multiple AI calls + ONE signal with cost tracing = Costs attributed to that signal
- Multiple signals = Only one should have
enable_cost_tracing=True
Basic Integration Pattern
Here’s the recommended pattern for integrating cost tracking with signals:
Python - Decorator
Python - Manual Trace
Node.js
Go
Ruby
Real-World Example: AI SDR Agent
Let’s walk through a complete example of an AI SDR agent that processes email replies and attributes costs to the appropriate signal.
Python
Node.js
Go
Ruby
Event Loop Integration
Most AI agents run in an event loop, processing events continuously. Here’s how to integrate cost tracking in that pattern:
Python
Node.js
Go
Ruby
Supported AI Provider Wrappers and Hooks
The following AI provider wrappers and hooks are available for cost tracking:
Python SDK
PaidOpenAI
/PaidAsyncOpenAI
- OpenAI APIPaidOpenAIAgentsHook
- Openai Agents hookPaidAnthropic
/PaidAsyncAnthropic
- Anthropic Claude APIPaidMistral
- Mistral AI APIPaidBedrock
- AWS BedrockPaidGemini
- Google GeminiPaidLlamaIndexOpenAI
- LlamaIndex with OpenAIPaidLangChainCallback
- LangChain hook
Node.js SDK
PaidOpenAI
- OpenAI API (import from@paid-ai/paid-node/openai
)PaidAnthropic
- Anthropic Claude API (import from@paid-ai/paid-node/anthropic
)PaidMistral
- Mistral AI API (import from@paid-ai/paid-node/mistral
)PaidLangChainCallback
- LangChain callback handler (import from@paid-ai/paid-node/langchain
)- Vercel AI SDK wrapper (import from
@paid-ai/paid-node/vercel
)
Best Practices
1. One Signal, One Outcome
Each trace should represent a single logical business outcome with one cost-enabled signal:
2. Wrap the Entire Event Handler
Make sure your tracing context includes all AI calls and the signal:
Troubleshooting
Costs Not Appearing in Dashboard
If costs aren’t showing up linked to your signals:
- Check tracing is initialized: Call
client.initialize_tracing()
or use@paid_tracing
decorator - Verify wrapper usage: Make sure you’re using
PaidOpenAI
not regularOpenAI
- Confirm signal is in trace: The signal must be emitted within the traced function
- Check enable_cost_tracing flag: Make sure it’s set to
True
for the signal - Verify external_agent_id: This is required when emitting signals
Double-Counted Costs
If costs appear multiple times:
- Check that only ONE signal per trace has
enable_cost_tracing=True
- Make sure you’re not accidentally calling the traced function multiple times
Missing AI Provider Costs
If some AI calls aren’t being tracked:
- Ensure all AI client calls are using the Paid wrapper or a hook
- Check that the wrapper supports your AI provider (see supported wrappers above)
- Verify the wrapper is created before entering the trace
Next Steps
Need Help?
- Email: support@paid.ai
- Community: Join
#paid-developers
on Slack