Skip to main content
cascadeflow integrates with LangChain through a callback handler that wraps any BaseChatModel. It keeps the product direction intact inside LangChain and LangGraph: decisions happen inside agent execution, with budgets, traces, and runtime policy visible where the workflow actually runs.

Install

pip install "cascadeflow[langchain]"

Quick Start

import cascadeflow
from cascadeflow.integrations.langchain import get_harness_callback
from langchain_openai import ChatOpenAI

cascadeflow.init(mode="observe")

model = ChatOpenAI(model="gpt-4o")
cb = get_harness_callback()

with cascadeflow.run(budget=0.50) as session:
    result = await model.ainvoke("Explain quantum computing", config={"callbacks": [cb]})
    print(session.summary())

Features

  • Full LCEL support (pipes, sequences, batch)
  • Streaming with pre-routing
  • Tool calling and structured output
  • LangSmith cost tracking metadata
  • Cost tracking callbacks
  • Domain policies with cascadeflow_domain metadata

Why This Integration Matters

  • Keeps LangChain apps framework-native instead of forcing a proxy hop
  • Makes runtime cost, latency, and trace data visible at the chain or agent level
  • Lets teams move from observability to governance without rewriting chain logic

Cost Tracking Callback

from cascadeflow.integrations.langchain.langchain_callbacks import get_cascade_callback

with get_cascade_callback() as cb:
    response = await cascade.ainvoke("What is Python?")
    print(f"Total cost: ${cb.total_cost:.6f}")
    print(f"Drafter cost: ${cb.drafter_cost:.6f}")
    print(f"Verifier cost: ${cb.verifier_cost:.6f}")

LangSmith Integration

When LangSmith tracing is enabled, cascadeflow adds metadata to runs:
  • cascade_decision: whether the drafter was accepted
  • modelUsed: which model produced the final response
  • drafterQuality: quality score from validation
  • savingsPercentage: cost savings achieved
export LANGSMITH_API_KEY="..."
export LANGSMITH_PROJECT="my-project"
export LANGSMITH_TRACING=true