langchainpy-langchain

Force LangChain agent to use a tool


Is there a way to force a LangChain agent to always use a tool?

(my specific use case: I need the agent to look for information in my database as opposed to generally accessible information and my tool searches through my database)


Solution

  • When running an agent query, you can explicitly mention about the custom tool.

    agent(
             "Using Custom Tool please calculate result of 5"
         )
    

    Also, in your _run() function, you add custom log for tracking when custom tool is been called.