Openrouter Function Calling
Overview
This skill demonstrates implementing function calling and tool use patterns with OpenRouter-supported models.
Prerequisites
- OpenRouter integration
- Model that supports function calling (GPT-4, Claude, etc.)
Instructions
- Define tool schemas: Create a
toolsarray with JSON Schema definitions for each function, specifying name, description, and parameter types - Send a request with tools: Include the
toolsarray in your chat completion request; settool_choice: "auto"to let the model decide when to call functions - Parse tool calls: Extract
tool_callsfromresponse.choices[0].message, deserialize eachfunction.argumentsJSON string - Execute and return results: Run each function locally, then send a follow-up request with
role: "tool"messages containing the results - Handle multi-turn loops: Continue the request-execute-respond cycle until the model returns a final text response without tool calls
Output
- Model-generated function calls with properly typed arguments
- Multi-turn conversation with tool execution results fed back to the model
- Final synthesized response incorporating tool outputs
Error Handling
| Error | Cause | Fix |
|---|---|---|
tool_calls is null |
Model chose not to call tools or doesn't support them | Check model compatibility; use tool_choice: "required" to force |
| JSON parse error on arguments | Model generated malformed JSON | Wrap JSON.parse in try/catch; retry with a more capable model |
| 400 invalid tool schema | Schema has unsupported types or format | Use only JSON Schema draft-07 types; avoid $ref or complex constructs |
See ${CLAUDE_SKILL_DIR}/references/errors.md for full error reference.
Examples
See ${CLAUDE_SKILL_DIR}/references/examples.md for runnable code samples.