Skip to main content

Goals

  • Use tool calling to build your own AI agent with Friendli Model APIs
  • Feel free to make your own custom tools!

Getting Started

  1. Head to Friendli Suite, and create an account.
  2. Grab a Personal API Key to use Friendli Model APIs within an agent.

Step 1. Playground UI

Experience tool calling on the Playground!
  1. On your left sidebar, click the ‘Model APIs’ option to access the playground page.
  2. You will see models that can be used as Model APIs. Select the one you want and click the endpoint.
  3. Click the ‘Tools’ button, select the Search tool, and enter a query to see the response. 😀

Step 2. Build a Custom Tool

Build your own creative tool. We will show you how to make a custom tool that retrieves temperature information. (Completed code snippet is provided at the bottom)
  1. Define a function for using as a custom tool
def get_temperature(location: str) -> int:
    """Mock function that returns the city temperature"""
    if "new york" in location.lower():
        return 45
    if "san francisco" in location.lower():
        return 72
    return 30
  1. Send a function calling inference request
    1. Add your input as a user role message.
    2. The information about the custom function (e.g., get_temperature) goes into the tools option. JSON schema describes the function’s parameters.
    3. The response includes the arguments field, which are values extracted from the user’s input that can be used as parameters of the custom function.
    # pip install friendli
    
    import os
    from friendli import SyncFriendli
    
    token = os.environ.get("API_KEY") or "YOUR_API_KEY"
    client= SyncFriendli(token=token)
    user_prompt = "I live in New York. What should I wear for today's weather?"
    
    messages = [
        {
            "role": "user",
            "content": user_prompt,
        },
    ]
    
    tools=[
        {
            "type": "function",
            "function": {
                "name": "get_temperature",
                "description": "Get the temperature information in a given location.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The name of current location e.g., New York",
                        },
                    },
                },
            },
        },
    ]
    
    chat = client.serverless.chat.complete(
        model="zai-org/GLM-5.2",
        messages=messages,
        tools=tools,
        temperature=0,
        frequency_penalty=1,
    )
    
    print(chat)
    
  2. Generate the final response using the tool calling results
    1. Add the tool_calls response as an assistant role message.
    2. Add the result obtained by calling the get_temperature function as a tool message to the Chat API again.
    import json
    
    func_kwargs = json.loads(chat.choices[0].message.tool_calls[0].function.arguments)
    temperature_info = get_temperature(**func_kwargs)
    
    messages.append(
        {
            "role": "assistant",
            "tool_calls": [
                tool_call.model_dump()
                for tool_call in chat.choices[0].message.tool_calls
            ]
        }
    )
    messages.append(
        {
            "role": "tool",
            "content": str(temperature_info),
            "tool_call_id": chat.choices[0].message.tool_calls[0].id
        }
    )
    
    chat_w_info = client.serverless.chat.complete(
        model="zai-org/GLM-5.2",
        tools=tools,
        messages=messages,
    )
    
    for choice in chat_w_info.choices:
        print(choice.message.content)
    
  • Complete Code Snippet
    # pip install friendli
    
    import json
    import os
    from friendli import SyncFriendli
    
    token = os.environ.get("API_KEY") or "YOUR_API_KEY"
    client = SyncFriendli(token=token)
    user_prompt = "I live in New York. What should I wear for today's weather?"
    
    messages = [
        {
            "role": "user",
            "content": user_prompt,
        },
    ]
    
    tools=[
        {
            "type": "function",
            "function": {
                "name": "get_temperature",
                "description": "Get the temperature information in a given location.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The name of current location e.g., New York",
                        },
                    },
                },
            },
        },
    ]
    
    chat = client.serverless.chat.complete(
        model="zai-org/GLM-5.2",
        messages=messages,
        tools=tools,
        temperature=0,
        frequency_penalty=1,
    )
    
    def get_temperature(location: str) -> int:
        """Mock function that returns the city temperature"""
        if "new york" in location.lower():
            return 45
        if "san francisco" in location.lower():
            return 72
        return 30
    
    func_kwargs = json.loads(chat.choices[0].message.tool_calls[0].function.arguments)
    temperature_info = get_temperature(**func_kwargs)
    
    messages.append(
        {
            "role": "assistant",
            "tool_calls": [
                tool_call.model_dump()
                for tool_call in chat.choices[0].message.tool_calls
            ]
        }
    )
    messages.append(
        {
            "role": "tool",
            "content": str(temperature_info),
            "tool_call_id": chat.choices[0].message.tool_calls[0].id
        }
    )
    
    chat_w_info = client.serverless.chat.complete(
        model="zai-org/GLM-5.2",
        tools=tools,
        messages=messages,
    )
    
    for choice in chat_w_info.choices:
        print(choice.message.content)
    

Congratulations

Following the above instructions, we’ve experienced the whole process of defining and using a custom tool to generate an accurate and rich answer from LLM models! Brainstorm creative ideas for your agent by reading our blog articles!
Last modified on July 6, 2026