---
title: "smolagents OpenAIModel with a custom api_base | BroadRouter"
description: "Connect Hugging Face smolagents to BroadNet Inference with OpenAIModel and api_base, for CodeAgent on any model or ToolCallingAgent on GLM 5.3 and Qwen3.8 27B."
url: https://broadrouter.com/guides/smolagents-openai-compatible/
lang: en
updated: 2026-11-16
publisher: BroadRouter (a BroadNet site); API by BroadNet Inference, https://inference.broadnet.ai
---

# Run smolagents on BroadNet Inference with OpenAIModel

Updated 2026-11-16

Create smolagents' OpenAIModel with model\_id set to a BroadNet ID such as broadnet/glm-5.3, api\_base set to https://inference.broadnet.ai/v1 and api\_key set to your BroadNet key, then pass it to CodeAgent or ToolCallingAgent. CodeAgent writes Python instead of native tool calls, so it can run on DeepSeek V4.1 Flash. ToolCallingAgent needs GLM 5.3 or Qwen3.8 27B.

smolagents is Hugging Face’s small Python library for agents. Its `OpenAIModel` class “connects to an OpenAI-compatible API server”, with `api_base` for the URL. BroadNet Inference serves chat completions at `https://inference.broadnet.ai/v1`, so it works directly. BroadRouter is BroadNet’s own site; this follows the smolagents 1.26.0 docs.

## Install

```bash
pip install smolagents
export BROADNET_API_KEY='your-broadnet-key'
```

## CodeAgent

```python
import os
from smolagents import OpenAIModel, CodeAgent

model = OpenAIModel(
    model_id="broadnet/deepseek-v4.1-flash",
    api_base="https://inference.broadnet.ai/v1",
    api_key=os.environ["BROADNET_API_KEY"],
    max_tokens=4000,
)

agent = CodeAgent(tools=[], model=model)
print(agent.run("What is 6 * 7? Use Python to check."))
```

`CodeAgent` asks the model to write Python between `<code>` tags and runs it locally. The request is a plain chat completion with stop sequences such as `</code>`, and no `tools` field. That means it doesn’t depend on native tool calling, so it can run on `broadnet/deepseek-v4.1-flash`, the model on BroadNet’s [unlimited plans](https://broadrouter.com/guides/unlimited-llm-api-sessions/). Code quality still varies by model, so test your own tasks.

CodeAgent runs code the model writes. Read smolagents’ guidance on secure code execution before you give it tools that touch files, networks or credentials.

## ToolCallingAgent

```python
from smolagents import ToolCallingAgent, tool

@tool
def get_weather(city: str) -> str:
    """Get the weather for a city.

    Args:
        city: The city name.
    """
    return f"21C in {city}"

model = OpenAIModel(
    model_id="broadnet/glm-5.3",
    api_base="https://inference.broadnet.ai/v1",
    api_key=os.environ["BROADNET_API_KEY"],
    max_tokens=4000,
)
agent = ToolCallingAgent(tools=[get_weather], model=model)
print(agent.run("What's the weather in Riyadh?"))
```

`ToolCallingAgent` sends your tools, plus its own `final_answer` tool, as function schemas with `tool_choice: "required"`. Use `broadnet/glm-5.3` or `broadnet/qwen3.8-27b`; BroadNet’s docs list tools for these two models only.

## Which model to pick

| Agent | Model |
| --- | --- |
| `CodeAgent`, long runs on a flat-price plan | `broadnet/deepseek-v4.1-flash` |
| `ToolCallingAgent` | `broadnet/glm-5.3` |
| Either agent with image input | `broadnet/qwen3.8-27b` |

A plan session runs one request at a time, and an agent sends its steps one after another, so one session fits one agent. [Prices for every model](https://broadrouter.com/pricing/).

## How we tested this

On 2026-11-15 we installed smolagents 1.26.0 in a clean virtual environment and ran both agents against a local mock server that speaks BroadNet’s chat-completions shape. The `CodeAgent` on the DeepSeek ID received a code reply, ran it and returned 42. Its requests had `max_tokens: 4000`, stop sequences and no tools. For the `ToolCallingAgent`, we checked the request shape: the model ID was sent unchanged, with both tool schemas and `tool_choice: "required"`. smolagents sends tool results back as user messages. Our mock didn’t follow that format, so we didn’t run that agent to a final answer. We had no BroadNet key, so we did not test real model output.

## Troubleshooting

-   **401.** The key is wrong, expired or out of allowance.
-   **404.** `api_base` must end in `/v1`. Check the model ID. Paid models need a top-up; the $5 credit covers only `broadnet/gemma-4-26b-a4b` and the two Transcribe models.
-   **“Make sure to provide correct code blobs”.** The model’s reply had no code block. Raise `max_tokens`, because reasoning counts toward it, or try another model. [Why answers come back empty](https://broadrouter.com/guides/reasoning-tokens-empty-response/).
-   **ToolCallingAgent never finishes.** Switch to `broadnet/glm-5.3` or `broadnet/qwen3.8-27b`.
-   **429.** A pay-as-you-go key allows 30 requests a minute. smolagents can throttle itself with `requests_per_minute`. See [errors and retries](https://broadrouter.com/guides/errors-and-retries/).

## Terms in this guide

[Tool calling](https://broadrouter.com/glossary/tool-calling/)

Tool calling, also called function calling, is an LLM API feature in which the model returns a structured request to run a function the developer has defined, with JSON arguments. The application runs the function and sends the result back; the model never runs code itself.

[Stop sequences](https://broadrouter.com/glossary/stop-sequences/)

Stop sequences are strings you send with a request that make the model stop generating as soon as it produces one of them. OpenAI calls the field stop and accepts up to 4 sequences; Anthropic calls it stop\_sequences. The matched text is normally left out of the returned output.

[Pay-as-you-go](https://broadrouter.com/glossary/pay-as-you-go/)

Pay-as-you-go is a pricing model in which you pay only for what you use, metered per unit such as tokens, requests or audio minutes, with no subscription or minimum commitment. Most LLM APIs bill this way, from a prepaid balance or a periodic invoice.

[Chat Completions API](https://broadrouter.com/glossary/chat-completions-api/)

The Chat Completions API is OpenAI's HTTP endpoint, POST /v1/chat/completions, that takes a model name and a list of role-tagged messages and returns the model's next message. Its request and response shape became the common format that OpenAI-compatible providers and SDKs reuse.

[OpenAI-compatible API](https://broadrouter.com/glossary/openai-compatible-api/)

An OpenAI-compatible API is an HTTP API that accepts the same request and response formats as the OpenAI API, such as POST /v1/chat/completions, so OpenAI SDKs and tools can use it by changing only the base URL, the API key and the model name.

[Prepaid credit](https://broadrouter.com/glossary/prepaid-credit/)

Prepaid credit is money added to an API account before use. Each request deducts its cost from the balance, and when the balance runs out, metered requests stop until you top up. It caps spend at what you have paid in.

[Rate limit](https://broadrouter.com/glossary/rate-limit/)

A rate limit is a cap an API places on how much a client can use it in a time window, such as requests per minute or tokens per minute. Requests over the limit are refused, usually with HTTP 429 Too Many Requests, until usage falls back under it.

## Questions

### Is it OpenAIModel or OpenAIServerModel?

smolagents 1.26 documents OpenAIModel for any OpenAI-compatible server. OpenAIServerModel is still exported by the package, but the docs describe OpenAIModel.

### Can smolagents run on the BroadNet unlimited plan?

The plans cover DeepSeek V4.1 Flash. BroadNet doesn't list tool calling for it, but CodeAgent doesn't need native tool calls; it asks the model for Python code. In our mock test a CodeAgent on the DeepSeek ID sent plain chat requests with no tools field.

### Why does ToolCallingAgent need GLM 5.3 or Qwen3.8 27B?

It sends the tools as OpenAI function schemas with tool\_choice set to required. BroadNet's docs list tools for GLM 5.3 and Qwen3.8 27B only.

### How do I pass max\_tokens or temperature?

As keyword arguments to OpenAIModel. smolagents' docs say extra arguments are forwarded to every completion call. In our test max\_tokens=4000 was sent on each request.

## Related guides

-   [Use the OpenAI Python SDK with BroadNet Inference](https://broadrouter.com/guides/openai-python-sdk/)
-   [How the unlimited LLM API plans work for agents](https://broadrouter.com/guides/unlimited-llm-api-sessions/)
-   [Why a reasoning model returns an empty answer](https://broadrouter.com/guides/reasoning-tokens-empty-response/)
-   [BroadNet Inference error codes and when to retry](https://broadrouter.com/guides/errors-and-retries/)
-   [All tools that work with BroadNet Inference](https://broadrouter.com/integrations/): SDKs, coding agents, chat UIs and automation tools, with exact settings for each

Try it on your real workload. New accounts start with $5 of credit for Gemma and transcription. DeepSeek, GLM and Qwen3.8 need a top-up.

[Get started](https://inference.broadnet.ai/account/?utm_source=broadrouter&utm_medium=referral&utm_campaign=guides-smolagents-openai-compatible&utm_content=page-cta) ·

**Sources**

-   [smolagents docs v1.26.0, Models reference (OpenAIModel) (checked 2026-11-15)](https://huggingface.co/docs/smolagents/reference/models)
-   [PyPI, smolagents 1.26.0 (checked 2026-11-15)](https://pypi.org/project/smolagents/)
-   [BroadNet Inference API reference (checked 2026-11-15)](https://inference.broadnet.ai/docs?utm_source=broadrouter&utm_medium=referral&utm_campaign=guides-smolagents-openai-compatible&utm_content=sources)
