langgoap.create_goap_agent#
- create_goap_agent(tools, goal, *, llm=None, preconditions=None, effects=None, resources=None, costs=None, result_keys=None, max_retries=None, durations=None, effect_validators=None, **graph_kwargs)[source]#
Create a compiled GOAP agent from a list of LangChain tools and a goal.
NL-at-invocation-time limitation: the returned
CompiledStateGraphonly exposes LangGraph’sinvoke({"goal": GoalSpec, "world_state": dict})andainvokeinterfaces. It does not haveinvoke_nl(), because that method lives on theGoapGraphbuilder, not on the compiled graph. For single-shot NL execution passgoal=request_stringdirectly (NL interpretation happens once at construction). For repeated NL-driven invocations against the same tool set, construct aGoapGraphdirectly and useinvoke_nl().- Parameters:
tools (list[BaseTool]) – The LangChain tools the agent may call.
goal (str | GoalSpec) – A
GoalSpec, or a natural-language string. If a string,llmmust be provided.llm (BaseChatModel | None) – Chat model used to interpret a string goal. Ignored when
goalis already aGoalSpec.preconditions (dict[str, dict[str, Any]] | None) – Optional mapping of tool name → preconditions dict. Missing tools get empty preconditions.
effects (dict[str, dict[str, Any]] | None) – Optional mapping of tool name → effects dict. Missing tools get empty effects.
resources (dict[str, dict[str, float]] | None) – Optional mapping of tool name → resources dict.
costs (dict[str, float] | None) – Optional mapping of tool name → action cost override.
result_keys (dict[str, str] | None) – Optional mapping of tool name → world-state key that should receive the tool’s raw return value at execution time. Use this to wire one tool’s output into the next tool’s input — e.g.
{"research_topic": "brief"}makes the return value ofresearch_topicavailable to a downstreamwrite_article(brief)tool viaworld_state["brief"]. Planning is unaffected; A* still reasons over the boolean flags ineffects. A key here must not collide with any key declared for the same tool ineffects(seegoapify_tool()).max_retries (dict[str, int] | None) – Optional mapping of tool name → planner-level retry budget.
0(default) blacklists the action on its first failure;NallowsNextra planner-level replans before blacklisting (i.e.N + 1total failures). This is the knob that lets a transient failure trigger replanning without giving up entirely.durations (dict[str, timedelta] | None) – Optional mapping of tool name →
datetime.timedelta. Used by the CSP scheduler when computing parallel execution windows. Tools with no entry default to instantaneous actions.effect_validators (dict[str, Callable[[dict[str, Any], dict[str, Any]], bool]] | None) – Optional mapping of tool name → callable
(pre_state, post_state) -> boolinvoked after execution. ReturningFalsesignals the action did not produce its declared effects (soft failure → replan).**graph_kwargs (Any) – Forwarded to
GoapGraph.compile()(e.g.checkpointer,store).
- Returns:
A compiled
CompiledStateGraphready for.invoke()or.ainvoke().- Raises:
ValueError – If
goalis a string butllmisNone.- Return type:
CompiledStateGraph