OpenCost Documentation
repository·develop·Indexed 27 days ago
https://github.com/opencost/opencostAn open-source cost monitoring tool for Kubernetes and cloud spend. OpenCost provides visibility into real-time and historical resource allocation, multi-cloud costs, and AI inference costs. It features a Model Context Protocol (MCP) server for AI agents, a currency conversion package for ISO 4217 currencies, and supports data sources including the OpenCost Collector and Prometheus (including sharded HA setups via Thanos, Cortex, or Mimir).
What's inside OpenCost
- The OpenCost Collector is a data source implementation that provides the metrics and metadata necessary for OpenCost to calculate cost allocation. It functions by gathering data from multiple sources—including Kubernetes, cloud providers, and other external systems—and transforming that data into a standardized format compatible with the OpenCost API.
Understand OpenCost Glossary and Core Concepts
developOpenCost uses standard Kubernetes and cloud terminology to define cost-incurring entities. Key concepts include:
- Cluster Assets: Observable entities in a Kubernetes cluster that directly incur costs (e.g., nodes, persistent volumes, attached disks, load balancers).
- Pod: A Kubernetes-specific concept consisting of a group of containers, treated as a single block of resources for scheduling and scaling.
- Namespace: A virtual cluster within Kubernetes used to deploy and observe pods/containers discreetly.
- Pod Labels: Key/Value pairs used to identify objects and group multiple namespaces associated with a specific workload.
- Container: An instance of a container image; the smallest identifiable unit of resource usage.
Understand Inference Cost Bases: Allocation vs Usage
developOpenCost calculates inference costs using two different bases:
- Allocation (
cost_basis=allocation): Includesmax(request, usage) × priceplus idle and shared infrastructure shares. Use this for chargeback, showback, and bill reconciliation. - Usage (
cost_basis=usage): Includes only actual resource consumption. Use this for pure workload efficiency analysis.
Note that
usagedoes not reconcile to the infrastructure bill as it excludes idle and shared infrastructure costs.- Allocation (
Understand the OpenCost cost measurement methodology
developThe OpenCost Specification provides a vendor-neutral methodology for measuring and allocating Kubernetes infrastructure and container costs. It categorizes costs into three main pillars:
- Total Cluster Costs = Cluster Asset Costs + Cluster Overhead Costs.
- Cluster Asset Costs = Resource Allocation Costs (provisioned/reserved) + Resource Usage Costs (consumed per unit).
- Asset Costs Distribution = Workload Costs (attributed to tenants) + Cluster Idle Costs (unallocated resources).
This hierarchy allows organizations to distinguish between what is being paid for (Assets), what is being used by workloads, and what is being wasted (Idle).
Understand AI Inference Cost Calculation Methodology
developOpenCost calculates inference costs using two primary methods:
1. Compute-Time Based Allocation (Default)
OpenCost splits infrastructure costs between input (prefill) and output (decode) based on processing time:
InputCost = TotalCost × (PrefillTime / TotalTime)OutputCost = TotalCost × (DecodeTime / TotalTime)
If vLLM timing metrics are unavailable, OpenCost falls back to a multiplier method where output tokens are assumed to cost 2.5× input tokens (
allocationMethod=multiplier).2. KV Cache Savings
OpenCost reports
cacheSavingsFraction(range 0–1) using the ratio ofcachedTokens / promptTokens. This is derived from thevllm:prefix_cache_hits_totalmetric. This fraction makes the cost benefit of KV caching explicit in the API.Understand the Modular OpenCost Architecture
developOpenCost is transitioning to a modular architecture to decouple core cost calculation logic from specific data sources. The architecture relies on three primary components:
- Metric Collection and Queries: Traditionally uses Prometheus to scrape emitters and expose metrics required for cost calculation. The goal is to replace the direct Prometheus dependency with a
DataSourcecontract. - Cloud Provider: An abstraction that provides specific cost data for a given resource, used to calculate the cost of that resource.
- Kubernetes API: Acts as the glue between raw metric data (from the metric source) and cost data (from the Provider).
By implementing a
DataSourcecontract, OpenCost aims to allow substitution of Prometheus with other sources like InfluxDB or in-memory sources for testing, without refactoring the core implementation.- Metric Collection and Queries: Traditionally uses Prometheus to scrape emitters and expose metrics required for cost calculation. The goal is to replace the direct Prometheus dependency with a
Understand the OpenCost Specification
developThe OpenCost specification defines the vendor-neutral requirements and expectations for implementing OpenCost monitoring within Kubernetes environments. It ensures interoperability between different cost monitoring tools and cloud providers by standardizing how cost data is collected and reported.Get support for OpenCost
developFor support or questions regarding contributions to OpenCost, you can join the #opencost channel on the CNCF Slack. Additionally, you can attend the biweekly OpenCost Working Group community meeting as listed in the Community Calendar.Enable and Configure the OpenCost MCP Server
developThe Model Context Protocol (MCP) server is disabled by default. It provides AI agents with access to cost allocation, asset, and cloud cost data via HTTP on port 8081. You can enable it and configure it using Helm
--setflags.Common Configuration Options:
opencost.mcp.enabled=true: Enables the MCP server.opencost.mcp.port: Sets a custom port (default is 8081).opencost.mcp.extraEnv.MCP_LOG_LEVEL: Sets the logging level (e.g.,debug).
Enable AI Inference Cost Tracking in OpenCost
developTo activate the inference cost tracking feature, set the following environment variable on your OpenCost deployment:
INFERENCE_COST_ENABLED=trueWhen enabled, OpenCost initializes the collector, calculator, exporter, and runner to process vLLM metrics and compute token-based costs.
Access OpenCost FAQ
developA list of commonly asked questions regarding OpenCost can be found in the official OpenCost documentation.Implement a custom DataSource contract
developThe proposed
DataSourcecontract is intended to provide an abstract interface for metric collection and querying. This allows developers to substitute Prometheus with other metric sources (e.g., InfluxDB or in-memory sources for testing) while maintaining the existing OpenCost data model and metric emission process.In the modular structure, each data source can be maintained within its own Go module inside the
opencostrepository. This allows consumers to select only the specific modules they require rather than inheriting the entire dependency tree.