0Pricing
AI Agents · Lesson

Choosing Tools at Runtime

Let the model decide which tool to call, force a specific tool, or expose only the tools relevant to the current state.

Choosing Tools at Runtime is a free AI Agents lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Let the Model Pick

By default, you give the model a list of tools and it decides which one to call (or none). This is the standard mode and works well when:

  • You have 5-15 tools
  • Tools have clear, distinct descriptions
  • The right tool depends on the user input

tool_choice: auto

The default — model decides:

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=messages,
    tools=tools,
    tool_choice='auto',  # default; can be omitted
)

tool_choice: required

Force the model to call SOME tool (any of them):

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=messages,
    tools=tools,
    tool_choice='required',
)
# Model must return a tool_call, never a free-form answer

Forcing a Specific Tool

To force one specific tool, name it:

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=messages,
    tools=tools,
    tool_choice={'type': 'function', 'function': {'name': 'extract_entities'}},
)

tool_choice: none

Disable tools for one turn:

# After a tool result, you might want a pure text response:
response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=messages,
    tools=tools,
    tool_choice='none',
)

Trim Tool Lists by Context

Models pick worse from large tool sets. Expose only the tools relevant to the current state:

def relevant_tools(state):
    if state == 'authenticated':
        return [search_orders, refund_order, escalate]
    elif state == 'guest':
        return [search_products, contact_support]
    return []

Two-Stage Tool Selection

For 50+ tools, use a hierarchy:

  1. Outer agent picks a domain (orders / billing / shipping)
  2. Inner agent picks the specific tool within that domain

Each call sees only ~10 tools.

Vector-Search Over Tools

Embed each tool description; at runtime, embed the user query and retrieve the top-K tools to expose:

from openai import OpenAI
import numpy as np

query_emb = embed(user_query)
sims = [cosine(query_emb, tool.emb) for tool in all_tools]
top = [t for _, t in sorted(zip(sims, all_tools), reverse=True)[:10]]
# Send only these 10 to the model

Anthropic tool_choice

Anthropic uses the same concept with a different shape:

tool_choice = {'type': 'auto'}
print("auto:", tool_choice)
tool_choice = {'type': 'any'}                              # required
print("any (required):", tool_choice)
tool_choice = {'type': 'tool', 'name': 'get_weather'}      # specific
print("specific tool:", tool_choice)
tool_choice = {'type': 'none'}
print("none:", tool_choice)

Why Force a Specific Tool?

Useful when:

  • Validation — extract one specific structured field
  • Routing — the user clicked "refund" so call refund_order
  • Structured output — abuse tool calling to get JSON

Empty Tool Lists

If you pass tools=[] but tool_choice="required", you get an error. Always validate the tool list before sending.

Forcing JSON Output

Why might you force a specific tool call?

Recap

Use auto for most cases. Force tools for routing and structured extraction. Trim the tool list per state to keep selection sharp.

Frequently asked questions

Is the “Choosing Tools at Runtime” lesson free?

Yes — the full text of “Choosing Tools at Runtime” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.

What will I learn in “Choosing Tools at Runtime”?

Let the model decide which tool to call, force a specific tool, or expose only the tools relevant to the current state. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Agents?

No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Choosing Tools at Runtime” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Agents lesson?

Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. How Function Calling Works
  2. Defining Tool Schemas (JSON Schema)
  3. Choosing Tools at Runtime
  4. Returning Results to the Model
← Back to AI Agents