How I Built a Free LLM API Workflow with AgentRouter in Python
Developers keep searching for one thing: a free LLM API that is simple to integrate and reliable in production-style workflows. In this guide, I share exactly how I set up AgentRouter with a Python toolkit and got working CLI and library calls in minutes.
Launch Faster: Grab the Repo + $200 Free Credits
Start building right now. Use the toolkit source code and claim your lifetime free credits to test real model workflows today.
Why AgentRouter + Python?
AgentRouter exposes an Anthropic-Messages-compatible endpoint. That means you can keep a familiar request format while trying multiple models behind one gateway.
The tricky part is client fingerprinting. Some third-party clients can fail with unauthorized client detected. The toolkit solves this by patching request headers for AgentRouter traffic and by providing a reliable fallback path.
Gateway Basics
- Base URL:
https://agentrouter.org - Endpoint:
POST /v1/messages - Auth:
Authorization: Bearer <token>
Quick Setup
1. Install dependencies
pip install -r requirements.txt
2. Configure environment
copy .env.example .env
AGENTROUTER_TOKEN=your_token_here
AGENTROUTER_BASE_URL=https://agentrouter.org
ANTHROPIC_MODEL=claude-opus-4-8
3. Run from CLI
python -m agentrouter "Write a concise launch announcement"
python -m agentrouter -m claude-opus-5 "Explain RAG in simple terms"
python -m agentrouter --test
Python Library Usage
from agentrouter import ask, call_model
print(ask(prompt="What is AgentRouter in one sentence?"))
text, via = call_model(
"gpt-5.6-sol",
[{"role": "user", "content": "Reply with pong"}],
64,
)
print(via, text)
How the Toolkit Improves Reliability
- Loads configuration from
.envautomatically. - Uses environment-based token handling instead of hardcoded secrets.
- Handles missing token with a clean CLI error.
- Tries litellm first, then raw API fallback if response parsing fails.
- Supports changing the default model via
ANTHROPIC_MODEL.
Pro Tip
Keep -m MODEL for per-command model choice and set ANTHROPIC_MODEL for your day-to-day default. This gives fast iteration without editing code.
Troubleshooting
Missing AGENTROUTER_TOKEN
Create .env from .env.example, set the token, and re-run your command.
unauthorized client detected
This usually means your client headers do not match expected SDK behavior. Use the toolkit path or the official Anthropic SDK configuration.
Authentication failed
Verify token value, check for stale shell variables, and confirm AGENTROUTER_BASE_URL is correct.
Final Thoughts
If your goal is to ship quickly on top of a free LLM API, this setup is practical: one env file, one CLI, and one Python API surface. It is simple enough for prototypes and structured enough for real workloads.
A good developer workflow is not just about model quality, it is about predictable integration, clear errors, and fast iteration.
Explore more technical writeups in my blog, and connect with me on GitHub for backend and AI/LLM engineering projects.