0
0
LangChainframework~10 mins

Cost tracking across runs in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cost tracking across runs
Start Run
Initialize Cost Tracker
Execute LangChain Run
Calculate API Call Costs
Add Costs to Tracker
Save/Update Total Cost
End Run
Retrieve Total Cost for Reporting
This flow shows how cost tracking starts before a LangChain run, accumulates API call costs during execution, updates totals, and ends with a report.
Execution Sample
LangChain
from langchain.callbacks import get_openai_callback

with get_openai_callback() as cb:
    result = chain.run(input)
    print(cb.total_cost)
This code runs a LangChain chain while tracking the total API cost used during that run.
Execution Table
StepActionAPI Calls MadeCost This Step (USD)Total Cost So Far (USD)Output
1Start run and initialize cost tracker00.000.00Cost tracker ready
2Run chain with input20.0040.004Intermediate results generated
3Run additional chain step10.0020.006Final result produced
4Print total cost00.0000.0060.006 printed
5End run00.0000.006Run complete
💡 Run ends after printing total cost; total API cost tracked is 0.006 USD
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
total_cost0.000.0040.0060.0060.006
api_calls02333
Key Moments - 2 Insights
Why does total_cost increase only after API calls?
Because cost is calculated based on API usage during chain execution, as shown in steps 2 and 3 where API calls add cost.
Does printing the cost affect the total cost?
No, printing is just output and does not make API calls, so total_cost stays the same as seen in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the total_cost after step 3?
A0.006
B0.004
C0.002
D0.000
💡 Hint
Check the 'Total Cost So Far (USD)' column at step 3 in the execution table.
At which step does the first API call cost get added?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Cost This Step (USD)' column to see when cost first appears.
If the chain made one more API call at step 3, how would total_cost change?
ADecrease by 0.002 USD
BIncrease by about 0.002 USD
CStay the same
DReset to zero
💡 Hint
Refer to cost increments per API call in the execution table rows 2 and 3.
Concept Snapshot
Use get_openai_callback() as a context manager to track API costs during LangChain runs.
Inside the with block, run your chain.
Access cb.total_cost to get the cost for that run.
Costs accumulate only when API calls happen.
Print or save total_cost after run to monitor expenses.
Full Transcript
This visual execution shows how LangChain tracks API costs across runs. First, a cost tracker is initialized before the chain runs. Each API call during the chain adds to the total cost. After the run, the total cost is printed. Variables like total_cost and api_calls update step-by-step. Key moments clarify that costs only increase with API calls and printing does not affect cost. The quiz tests understanding of cost accumulation and tracking steps.