Bird
Raised Fist0
LangChainframework~10 mins

OpenAI functions agent in LangChain - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - OpenAI functions agent
User Input
Agent Receives Input
Agent Checks for Function Call
Call Function
Function Executes
Return Function Output
Agent Incorporates Output
Respond to User
The agent receives user input, decides if a function call is needed, executes it if yes, then responds with the function output or text.
Execution Sample
LangChain
from langchain.agents import create_openai_functions_agent, AgentExecutor

agent = create_openai_functions_agent(llm, tools)
agent_executor = AgentExecutor(agent=agent, tools=tools)
response = agent_executor.invoke({'input': 'What is the weather in Paris?'})
This code creates an OpenAI functions agent with executor and sends a user question to get a response, possibly calling a function.
Execution Table
StepActionInputAgent DecisionFunction CalledFunction OutputAgent Response
1Receive user inputWhat is the weather in Paris?Check if function needed
2Decide to call functionWhat is the weather in Paris?Yes, call weather functionget_weather(location='Paris')
3Execute functionlocation='Paris'Sunny, 25°C
4Incorporate function outputSunny, 25°CThe weather in Paris is Sunny, 25°C.
5Return response to userThe weather in Paris is Sunny, 25°C.
💡 Agent returns final response after function call and output incorporation.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
user_inputWhat is the weather in Paris?What is the weather in Paris?What is the weather in Paris?What is the weather in Paris?
agent_decisionCheck if function neededCall functionFunction output receivedResponse ready
function_calledget_weather(location='Paris')get_weather(location='Paris')get_weather(location='Paris')
function_outputSunny, 25°CSunny, 25°C
agent_responseThe weather in Paris is Sunny, 25°C.
Key Moments - 3 Insights
How does the agent know when to call a function instead of just replying?
The agent checks the input and matches it to available functions. If a function fits the request, it chooses to call it (see execution_table step 2).
What happens if the function output is empty or missing?
The agent waits for the function output before responding. If missing, it may fallback to a text response or error handling (not shown here).
Can the agent call multiple functions in one interaction?
Yes, but this example shows a single function call. The agent can chain calls by repeating the decision and execution steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what function does the agent call at step 2?
Asend_email(address='Paris')
Bget_news(topic='Paris')
Cget_weather(location='Paris')
DNo function called
💡 Hint
Check the 'Function Called' column at step 2 in the execution_table.
At which step does the agent receive the function output?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Function Output' column in the execution_table.
If the user input was 'Tell me a joke', what would likely change in the execution_table?
AAgent calls a joke function instead of weather
BAgent skips function call and replies directly
CAgent calls get_weather function anyway
DAgent crashes
💡 Hint
Think about when the agent decides to call a function or just reply (see agent decision in execution_table step 2).
Concept Snapshot
OpenAI functions agent:
- Receives user input
- Checks if a function call fits the request
- Calls the function if needed
- Gets function output
- Responds using output or text
- Enables dynamic, real-time info in conversations
Full Transcript
The OpenAI functions agent works by first receiving what the user says. It then decides if it needs to call a special function to answer. If yes, it calls that function with the right details. The function runs and sends back an answer. The agent uses this answer to reply to the user. If no function is needed, the agent just replies with text. This way, the agent can give smart answers using live data or tools.

Practice

(1/5)
1. What is the main purpose of an OpenAI functions agent in Langchain?
easy
A. To store large datasets for AI processing
B. To train new AI models from scratch
C. To create user interfaces for AI applications
D. To connect AI chat with your own custom functions for smarter responses

Solution

  1. Step 1: Understand the role of an OpenAI functions agent

    An OpenAI functions agent links AI chat capabilities with user-defined functions to perform tasks.
  2. Step 2: Compare options to the definition

    Only To connect AI chat with your own custom functions for smarter responses describes connecting AI chat with custom functions, which matches the agent's purpose.
  3. Final Answer:

    To connect AI chat with your own custom functions for smarter responses -> Option D
  4. Quick Check:

    Agent purpose = connect AI chat + functions [OK]
Hint: Remember: functions agent links AI chat to your code [OK]
Common Mistakes:
  • Confusing agent with AI model training
  • Thinking it stores data instead of connecting functions
  • Assuming it builds user interfaces
2. Which of the following is the correct way to create an OpenAI functions agent in Langchain?
easy
A. agent = OpenAIFunctionsAgent(functions, model)
B. agent = OpenAIChatAgent(model, funcs)
C. agent = OpenAIFunctionsAgent(llm=model, tools=funcs)
D. agent = FunctionsAgent(llm=model, funcs=functions)

Solution

  1. Step 1: Recall the correct constructor syntax

    The OpenAI functions agent requires named parameters: llm for the model and tools for the list of tools.
  2. Step 2: Check each option for correct names and syntax

    agent = OpenAIFunctionsAgent(llm=model, tools=funcs) uses correct class name and named parameters. Others either use wrong class names or positional arguments incorrectly.
  3. Final Answer:

    agent = OpenAIFunctionsAgent(llm=model, tools=funcs) -> Option C
  4. Quick Check:

    Correct constructor = agent = OpenAIFunctionsAgent(llm=model, tools=funcs) [OK]
Hint: Look for named parameters llm and tools in constructor [OK]
Common Mistakes:
  • Using positional arguments instead of named
  • Wrong class names like OpenAIChatAgent
  • Mixing parameter names like funcs vs tools
3. Given the code snippet:
from langchain.agents import OpenAIFunctionsAgent

model = OpenAI()
functions = [get_weather, get_news]
agent = OpenAIFunctionsAgent(llm=model, tools=functions)
response = agent.invoke({'input': 'What is the weather today?'})
print(response)

What will print(response) most likely output?
medium
A. A string response from the AI calling get_weather function
B. A syntax error due to missing parameters
C. An empty dictionary because no functions are called
D. A runtime error because invoke method does not exist

Solution

  1. Step 1: Understand agent.invoke behavior

    The agent uses the AI model and tools list to process input and call the right function, here likely get_weather.
  2. Step 2: Analyze the code flow

    Input asks about weather, so the agent calls get_weather and returns its result as a string response.
  3. Final Answer:

    A string response from the AI calling get_weather function -> Option A
  4. Quick Check:

    invoke calls function and returns response [OK]
Hint: Input about weather triggers get_weather function call [OK]
Common Mistakes:
  • Assuming invoke method does not exist
  • Expecting empty output without function calls
  • Confusing syntax errors with runtime behavior
4. What is wrong with this code snippet for creating an OpenAI functions agent?
model = OpenAI()
functions = [get_time]
agent = OpenAIFunctionsAgent(functions, model)
response = agent.invoke({'input': 'What time is it?'})
medium
A. The agent constructor is missing named parameters for llm and tools
B. The tools list should be a dictionary, not a list
C. The invoke method requires a string, not a dictionary
D. The OpenAI model must be passed as a string, not an object

Solution

  1. Step 1: Check constructor parameter usage

    The OpenAIFunctionsAgent requires named parameters: llm= and tools=, not positional arguments.
  2. Step 2: Verify other parts of the code

    Tools as list is correct, invoke accepts a dictionary input, and model is an object as expected.
  3. Final Answer:

    The agent constructor is missing named parameters for llm and tools -> Option A
  4. Quick Check:

    Constructor needs named params llm= and tools= [OK]
Hint: Always use named parameters llm= and tools= in constructor [OK]
Common Mistakes:
  • Passing positional arguments instead of named
  • Thinking tools must be a dictionary
  • Misunderstanding invoke input type
5. You want to build a Langchain app that answers user questions by calling either get_weather or get_news functions based on input. Which approach correctly sets up the OpenAI functions agent to handle this?
hard
A. Create two separate agents, one for weather and one for news, and switch manually
B. Pass both functions in a list to OpenAIFunctionsAgent and let it decide which to call
C. Use only get_weather function and ignore get_news for simplicity
D. Call functions directly without using an agent

Solution

  1. Step 1: Understand agent's function selection

    The OpenAI functions agent can receive multiple functions and uses AI to pick the right one based on input.
  2. Step 2: Evaluate options for best design

    Passing both functions in a list lets the agent decide automatically, which is the intended use.
  3. Final Answer:

    Pass both functions in a list to OpenAIFunctionsAgent and let it decide which to call -> Option B
  4. Quick Check:

    Agent selects function from list automatically [OK]
Hint: Give all functions to agent; it picks based on input [OK]
Common Mistakes:
  • Manually switching between agents instead of one agent
  • Ignoring needed functions for simplicity
  • Bypassing agent and losing AI routing benefits