Current collected state Current
Observed since 20 Oct, 21:32 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
agentbill-pcu-tools-2025
LangChain callback handler for automatic usage tracking and billing with AgentBill
Automatic usage tracking and billing for LangChain applications.
Install via pip:
pip install agentbill-langchain
With OpenAI support:
pip install agentbill-langchain[openai]
With Anthropic support:
pip install agentbill-langchain[anthropic]
from agentbill_langchain import AgentBillCallback
from langchain_openai import ChatOpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
# 1. Initialize AgentBill callback
callback = AgentBillCallback(
api_key="agb_your_api_key_here", # Get from AgentBill dashboard
base_url="https://bgwyprqxtdreuutzpbgw.supabase.co",
customer_id="customer-123",
debug=True
)
# 2. Create LangChain chain with callback
llm = ChatOpenAI(model="gpt-4o-mini")
prompt = PromptTemplate.from_template("Tell me a joke about {topic}")
chain = LLMChain(llm=llm, prompt=prompt)
# 3. Run - everything is auto-tracked!
result = chain.invoke(
{"topic": "programming"},
config={"callbacks": [callback]}
)
print(result["text"])
# ✅ Automatically captured:
# - Prompt text (hashed for privacy)
# - Model name (gpt-4o-mini)
# - Provider (openai)
# - Token usage (prompt + completion)
# - Latency (ms)
# - Costs (calculated automatically)
# Track revenue for profitability analysis
callback.track_revenue(
event_name="chat_completion",
revenue=0.50, # What you charged the customer
metadata={"subscription_tier": "pro"}
)
from langchain.agents import initialize_agent, load_tools
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(
tools,
llm,
agent="zero-shot-react-description",
callbacks=[callback] # Add callback here
)
# All agent steps auto-tracked!
response = agent.run("What is 25% of 300?")
from langchain.chains import SimpleSequentialChain
# All chain steps tracked automatically
overall_chain = SimpleSequentialChain(
chains=[chain1, chain2, chain3],
callbacks=[callback]
)
result = overall_chain.run(input_text)
callback = AgentBillCallback(
api_key="agb_...", # Required - get from dashboard
base_url="https://...", # Required - your AgentBill instance
customer_id="customer-123", # Optional - for multi-tenant apps
account_id="account-456", # Optional - for account-level tracking
debug=True, # Optional - enable debug logging
batch_size=10, # Optional - batch signals before sending
flush_interval=5.0 # Optional - flush interval in seconds
)
The callback hooks into LangChain's lifecycle:
All data is sent to AgentBill via the record-signals API endpoint with proper authentication.
Auto-cost calculation for:
debug=True to see logsbase_url matches your instanceMIT
Download counts and popularity will appear here as the collector receives them.
| Package / target | Requirement | Relationship |
|---|---|---|
langchainEnvironment{
"extras": "",
"marker": null,
"requires_python": ">=3.8",
"evaluation": "NOT_EVALUATED"
} | langchain>=0.1.0 | runtime |
requestsEnvironment{
"extras": "",
"marker": null,
"requires_python": ">=3.8",
"evaluation": "NOT_EVALUATED"
} | requests>=2.28.0 | runtime |
langchain-openaiEnvironment{
"extras": "",
"marker": "extra == \"openai\"",
"requires_python": ">=3.8",
"evaluation": "NOT_EVALUATED"
} | langchain-openai>=0.0.1; extra == "openai" | runtime |
langchain-anthropicEnvironment{
"extras": "",
"marker": "extra == \"anthropic\"",
"requires_python": ">=3.8",
"evaluation": "NOT_EVALUATED"
} | langchain-anthropic>=0.0.1; extra == "anthropic" | runtime |
bdist_wheel · 7.35 KB · Python py3
sdist · 7.02 KB · Python source
Observed since 20 Oct, 21:32 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package_version:449c602d31eb971ec44229bc4fa280e5b41771a62cf03e9335b58af45cac84bb