This guide covers billing and cost tracking for integrations. For a complete walkthrough of building an integration, see Building Your First Integration.
Integrations can report per-action costs back to Autohive for billing and usage tracking. This is useful for integrations that call paid third-party APIs where each execution has a measurable cost.
To enable billing support, add the supports_billing field to your integration's config.json:
{
"name": "my-integration",
"entry_point": "my_integration.py",
"description": "My integration with a paid API",
"supports_billing": true,
"auth": { ... },
"actions": { ... }
}- Field:
supports_billing - Type:
boolean - Required: No (defaults to
false) - Description: When
true, the integration's action handlers are expected to returnActionResultobjects that may include cost information via thecost_usdfield
The SDK provides the ActionResult dataclass for returning data along with optional billing information:
from autohive_integrations_sdk import ActionResultActionResult accepts two fields:
data— the actual result data from the actioncost_usd— optional USD cost for billing purposes (defaults toNone)
When your integration has supports_billing: true, your action handlers should return ActionResult with the cost_usd field to report the cost of each execution:
from autohive_integrations_sdk import Integration, ExecutionContext, ActionHandler, ActionResult
from typing import Dict, Any
integration = Integration.load()
@integration.action("call_api")
class CallApiAction(ActionHandler):
async def execute(self, inputs: Dict[str, Any], context: ExecutionContext) -> ActionResult:
url = inputs["url"]
credentials = context.auth.get("credentials", {})
api_key = credentials.get("api_key", "")
response = await context.fetch(url, headers={"Authorization": f"Bearer {api_key}"})
return ActionResult(
data={"result": response.data},
cost_usd=0.05
)cost_usdis afloatrepresenting the cost in US dollars- Set
cost_usd=0.0for actions that have no cost but still want to participate in billing tracking - Omit
cost_usdif billing information is not available for a particular execution
For integrations calling APIs with per-request pricing, calculate and report the actual cost:
@integration.action("generate_content")
class GenerateContentAction(ActionHandler):
async def execute(self, inputs: Dict[str, Any], context: ExecutionContext) -> ActionResult:
prompt = inputs["prompt"]
credentials = context.auth.get("credentials", {})
api_key = credentials.get("api_key", "")
response = await context.fetch(
"https://api.example.com/generate",
method="POST",
headers={"Authorization": f"Bearer {api_key}"},
json={"prompt": prompt}
)
# Calculate cost based on usage returned by the API
tokens_used = response.data.get("usage", {}).get("total_tokens", 0)
cost = tokens_used * 0.00001 # $0.01 per 1000 tokens
return ActionResult(
data={"content": response.data["result"]},
cost_usd=cost
)- Always return
ActionResultorActionErrorwhensupports_billingistrue— both support thecost_usdfield - Be accurate with costs - report the actual cost incurred by the third-party API call, not an estimate
- Use
0.0for free operations - if an action doesn't cost anything, explicitly returncost_usd=0.0to signal that billing is working correctly - Calculate dynamically when possible - if the API returns usage data (e.g., tokens consumed), use it to compute the cost rather than using a fixed value
- Track costs on errors - if a third-party API call was made but the action still failed, return
ActionErrorwithcost_usdto ensure the charge is captured
To add billing to an existing integration:
- Add
"supports_billing": truetoconfig.json - Update action handlers to return
ActionResultwithcost_usd - Import
ActionResultfrom the SDK:from autohive_integrations_sdk import ActionResult - Re-upload the integration