Learn how to see what your AI agents do and bill your customers fairly.
The Real Problem
You built an AI agent. Companies are paying you to use it. But here's the tricky part:
How do you know what to charge them?
Your AI agent does lots of things:
- It talks to ChatGPT or Claude (each call costs you money)
- It searches the internet
- It reads documents
- It sends emails
Every action has a cost. And you need to charge your customers based on what they actually use.
Think of it like a taxi. You don't charge everyone the same price. You charge based on how far they travel. Your AI agent is the same, you charge based on how much work it does.
Valmi Value is like the meter in a taxi. It counts everything your AI does, so you can charge customers fairly and know if you're making money.
What We'll Build
By the end of this guide, you'll have:
- A way to track every action your AI agent takes
- A dashboard showing who used what
- The data you need to bill customers based on actual usage
Before You Start
You'll need:
- Python 3.9 or newer
- Basic Python knowledge
Step 1: Sign Up
First, create your Valmi Value account at value.valmi.io.
This is where you'll manage your agents, customers, and billing.
Value - Sign Up
Step 2: Create a Customer Account
In business, you have customers. Each customer needs an account so you can track their usage separately.
Think of it like a gym. Each member has their own card. When they scan in, you know who's using the equipment and how much.
To create an account:
- Log into value.valmi.io
- Click Accounts in the sidebar
- Click Create Account
- Enter your customer's details:
- Name:
Acme Corp - Email:
billing@acme.com - Phone:
+1-555-123-4567 - Address:
123 Main Street, San Francisco, CA 94102 - Description:
Enterprise customer - AI research tools
- Name:
- Click Create
Now Acme Corp has their own account. Any AI work they do gets tracked separately from your other customers.
Value - Account Creation
Step 3: Add a Contact
Every customer account needs a person you can reach. This is who gets invoices and usage alerts.
- Open the account you just made
- Click Contacts tab
- Click Add Contact
- Fill in:
- Name:
John Doe - Email:
john@acme.com - Phone:
+1-555-987-6543 - Address:
123 Main Street, San Francisco, CA 94102 - Description:
Primary billing contact for AI services
- Name:
- Click Add
Now when you send Acme Corp their bill, John gets the email.
Value - Contact Creation
Step 4: Create an Agent Type
You might build different AI agents for different jobs:
- A customer support agent
- A research agent
- A data analysis agent
Each type is like a different product you sell. Let's create one.
- Click Agents in the sidebar
- Click Agent Types tab
- Click Create Agent Type
- Fill in:
- Name:
Research Agent - Description:
Researches topics and summarizes findings - Type:
Custom Agent(or choose LangGraph, CrewAI, etc.)
- Name:
- Click Create
This is like adding a new product to your catalog.
Value - Agent Type Creation
Step 5: Create an Agent Instance
An Agent Type is like a blueprint. An Agent Instance is an actual working copy.
Why does this matter? Because different customers might use the same type of agent, but you want to track them separately.
Think of it like a rental car company. "Toyota Camry" is the type. But each actual car has its own license plate so you know which customer has which car.
- Click Agents → Instances in the sidebar
- Click Create Instance
- Fill in:
- Name:
Acme Research Agent - Description:
Research agent instance for Acme Corp - Agent Type: Select
Research Agent - Account: Select
Acme Corp - Contact: Select
John Doe
- Name:
- Click Create
- Copy the Agent Secret — this is the key that identifies this specific agent
Important: The Agent Secret is like a password. Keep it safe. Don't put it directly in your code where others can see it.
Value - Agent Instance Creation
Step 8: Track Actions
Now the important part. Every time your agent does something billable, you record it.
Here's a real example: a research agent that:
- Searches for information
- Calls an LLM to summarize
- Sends results to the user
import os
from value import initialize_sync
AGENT_SECRET = os.getenv("VALUE_AGENT_SECRET")
client = initialize_sync(agent_secret=AGENT_SECRET)
def run_research(user_id: str, topic: str):
"""
A research agent workflow.
Each action is tracked so we know what to charge.
"""
# action_context ties all actions to a specific user
with client.action_context(user_id=user_id) as ctx:
# ACTION 1: Web search
# In real code, this would call a search API
search_results = perform_web_search(topic)
ctx.send(
action_name="web_search",
**{
"value.action.description": f"Searched for: {topic}",
"results_count": len(search_results),
"search_provider": "google",
}
)
# ACTION 2: LLM summarization
# This costs money - track the tokens
summary = call_llm_to_summarize(search_results)
ctx.send(
action_name="llm_call",
**{
"value.action.description": "Summarized search results",
"model": "gpt-4",
"input_tokens": 2500,
"output_tokens": 800,
"cost_usd": 0.12, # Track your actual cost
}
)
# ACTION 3: Deliver results
send_to_user(user_id, summary)
ctx.send(
action_name="delivery",
**{
"value.action.description": "Sent summary to user",
"delivery_method": "email",
}
)
return summary
# Run for a specific customer's user
run_research(user_id="user_acme_123", topic="market trends in AI")
What's happening here:
action_context(user_id=...)— Groups all actions under one user sessionctx.send(action_name=...)— Records each action with details- The metadata (tokens, cost, etc.) helps you understand usage and costs
Step 9: See the Data
Go to value.valmi.io and click Metering → Live Data.
You'll see every action in real-time:
| created_at | measure | tag | value_action_name | value_action_user_attributes |
|---|---|---|---|---|
| 2025-12-15T16:11:43 | - | - | delivery | {"delivery_method": "email"} |
| 2025-12-15T16:11:43 | @outcome | - | llm_call | {"model": "gpt-4", "input_tokens":... |
| 2025-12-15T16:11:43 | - | - | web_search | {"results_count": 2, "search_p... |
Now you know exactly:
- Which customer used your agent
- What actions were performed
- How much each action cost you
- Data to build usage-based billing
Value - Sending Agent Traces
Step 10: Auto-Track LLM Calls (Optional)
Manually tracking every LLM call is tedious. If you use Gemini or LangChain, enable auto-instrumentation:
from value import initialize_sync, auto_instrument
client = initialize_sync(agent_secret=AGENT_SECRET)
auto_instrument(["gemini"]) # Automatically tracks all Gemini calls
# Now just use Gemini normally — all calls are tracked automatically
from google import genai
gemini = genai.Client(api_key="your-key")
response = gemini.models.generate_content(
model="gemini-2.5-flash",
contents=["Analyze this market data..."]
)
Every LLM call is recorded with model name, tokens, and latency — no extra code needed.
The Business Value
With all this tracking data, you can now:
1. Bill Based on Usage
Charge customers for what they actually use. "You made 500 LLM calls this month at $0.002 each = $1.00"
2. Know Your Margins
You can see: "This customer paid us $100, but their LLM costs were $80. Our margin is only 20%."
3. Find Heavy Users
See which customers use the most resources. Maybe they need a higher plan.
4. Optimize Costs
Find which actions cost the most. Maybe switch to a cheaper model for simple tasks.
Best Practices
Name Actions Clearly
# Good — describes what happened
ctx.send(action_name="document_parsed")
ctx.send(action_name="customer_query_answered")
# Bad — meaningless
ctx.send(action_name="step_1")
ctx.send(action_name="done")
Track What Matters for Billing
ctx.send(
action_name="llm_call",
**{
"model": "gpt-4", # Which model (affects cost)
"input_tokens": 1500, # Input size
"output_tokens": 500, # Output size
"cost_usd": 0.045, # Your actual cost
"latency_ms": 1200, # Performance tracking
}
)
Always Identify the User
# Known customer user
with client.action_context(user_id="customer_123") as ctx:
...
# Anonymous/trial user
with client.action_context(anonymous_id="trial_session_abc") as ctx:
...
Keep Secrets Safe
Never hardcode secrets. Use environment variables:
export VALUE_AGENT_SECRET="your-secret"
import os
secret = os.environ["VALUE_AGENT_SECRET"]
What's Next?
Now that you're tracking actions, you can:
- Define Outcomes: Group actions into billable outcomes like "research completed"
- Set Up Pricing: Create pricing rules (per action, tiered, etc.)
- Allocate Costs: Attribute LLM costs to understand margins
- Generate Invoices: Bill customers based on actual usage
Summary
You learned how to:
- Create customer accounts to track usage separately
- Set up agent types and instances
- Track every action your AI agent takes
- View real-time usage data
- Enable automatic LLM tracking
This gives you the foundation for usage-based billing — charge customers fairly based on what they actually use, and know exactly what it costs you to serve them.
Resources
Questions? Reach out at contact@valmi.io