langgoap.CostAccumulator#
- class CostAccumulator(world_state, *, cost_per_1k_tokens=None, default_input_price=0.0, default_output_price=0.0, require_known_pricing=False)[source]#
Accumulate LLM cost / tokens / call count into a
world_statedict.Wire it on the LLM you call from your action’s
execute/aexecute:from langgoap import CostAccumulator from langchain_openai import ChatOpenAI def research(world_state: dict) -> dict: llm = ChatOpenAI( model='gpt-4o-mini', temperature=0, callbacks=[CostAccumulator(world_state)], ) response = llm.invoke('summarise these sources: ...') return {'summary': response.content}
After the call,
world_state['total_cost_usd'],['total_tokens'], and['llm_call_count']carry the running totals — exactly whatMaxCostPolicy/MaxTokensPolicy/MaxLLMCallsPolicyconsume.- Parameters:
world_state (dict[str, Any]) – The dict to write totals into. Mutated in place so the policies see live values across action invocations.
cost_per_1k_tokens (Mapping[str, tuple[float, float]] | None) – Optional pricing override (
model_namesubstring →(input_per_1k, output_per_1k)in USD). Merged withDEFAULT_COST_PER_1K_TOKENS— your entries win on collision.default_input_price (float) – Used when no model-specific entry matches. Defaults to
0.0; with both defaults at zero an unknown model is counted at $0 and aUserWarningis emitted once per model name so the gap is visible (issue #55).default_output_price (float) – As above.
require_known_pricing (bool) – When
True, an unknown model raisesValueErrorinstead of being priced at the defaults.
The bundled
DEFAULT_COST_PER_1K_TOKENSis a convenience snapshot, not a maintained price list: passcost_per_1k_tokensfor any model you rely on for budget enforcement.- __init__(world_state, *, cost_per_1k_tokens=None, default_input_price=0.0, default_output_price=0.0, require_known_pricing=False)[source]#
- Parameters:
world_state (dict[str, Any])
cost_per_1k_tokens (Mapping[str, tuple[float, float]] | None)
default_input_price (float)
default_output_price (float)
require_known_pricing (bool)
- Return type:
None
Methods
__init__(world_state, *[, ...])on_agent_action(action, *, run_id[, ...])Run on agent action.
on_agent_finish(finish, *, run_id[, ...])Run on the agent end.
on_chain_end(outputs, *, run_id[, parent_run_id])Run when chain ends running.
on_chain_error(error, *, run_id[, parent_run_id])Run when chain errors.
on_chain_start(serialized, inputs, *, run_id)Run when a chain starts running.
on_chat_model_start(serialized, messages, *, ...)Run when a chat model starts running.
on_custom_event(name, data, *, run_id[, ...])Override to define a handler for a custom event.
on_llm_end(response, *[, run_id, parent_run_id])Run when LLM ends running.
on_llm_error(error, *, run_id[, ...])Run when LLM errors.
on_llm_new_token(token, *[, chunk, ...])Run on new output token.
on_llm_start(serialized, prompts, *, run_id)Run when LLM starts running.
on_retriever_end(documents, *, run_id[, ...])Run when Retriever ends running.
on_retriever_error(error, *, run_id[, ...])Run when Retriever errors.
on_retriever_start(serialized, query, *, run_id)Run when the Retriever starts running.
on_retry(retry_state, *, run_id[, parent_run_id])Run on a retry event.
on_stream_event(event, *, run_id[, ...])Run on each protocol event from stream_events(version="v3").
on_text(text, *, run_id[, parent_run_id])Run on an arbitrary text.
on_tool_end(output, *, run_id[, parent_run_id])Run when the tool ends running.
on_tool_error(error, *, run_id[, parent_run_id])Run when tool errors.
on_tool_start(serialized, input_str, *, run_id)Run when the tool starts running.
Attributes
ignore_agentWhether to ignore agent callbacks.
ignore_chainWhether to ignore chain callbacks.
ignore_chat_modelWhether to ignore chat model callbacks.
ignore_custom_eventIgnore custom event.
ignore_llmWhether to ignore LLM callbacks.
ignore_retrieverWhether to ignore retriever callbacks.
ignore_retryWhether to ignore retry callbacks.
raise_errorWhether to raise an error if an exception occurs.
run_inlineWhether to run the callback inline.