0

Using the Anthropic messsages API directly and providing multiple tools to the model, it's possible to instruct the model to only invoke one tool at a time by setting disable_parallel_tool_use=true in the tool_choice field.

I'm using LangGraph, and I'd like to be able to do the same thing - instruct the model to only choose one tool at a time.

Below is the heart of my code. I'm leaving out the tool method definitions and prompts, because they're not directly relevant.

model = ChatAnthropic(model="claude-3-5-sonnet-20241022")
tools = [search_word_forms, search_dictionaries, WordDetermination]
model_with_tools = model.bind_tools(tools, tool_choice="any")
response = model_with_tools.invoke(state["messages"])

Using this framework, how can I instruct the model to disable parallel tool use?

2 Answers 2

2

Check out Langchain's document of ChatAnthropic: https://python.langchain.com/api_reference/anthropic/chat_models/langchain_anthropic.chat_models.ChatAnthropic.html#langchain_anthropic.chat_models.ChatAnthropic.bind_tools

You can use the dictionary containing options like type or disable_parallel_tool_use as tool_choice argument of bind_tools.

So the example code would be like:

model = ChatAnthropic(model="claude-3-5-sonnet-20241022")

tool_choice = {
    "type": "any" if tool_call_only else "auto",
    "disable_parallel_tool_use": not enable_parallel_tool_calls,
}
model_with_tools = model.bind_tools(tools, tool_choice=tool_choice)
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting! The documentation doesn't say that explicitly. The only values with dict type that it documents are: name of the tool as a string or as dict {"type": "tool", "name": "<<tool_name>>"}: calls corresponding tool;
I tested it out, and it seems like it works! I guess this is because LangChain directly gives the tool_choice directory to Claude request without modification? python.langchain.com/api_reference/_modules/langchain_anthropic/…
2

As of the latest version of langchain-anthropic you can specify parallel_tools_calls when calling bind_tools:

model = ChatAnthropic(model="claude-3-5-sonnet-20241022")

model_with_tools = model.bind_tools(tools, parallel_tool_calls=False)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.