[PR #14891] anthropic: support num_ctx parameter in /v1/messages endpoint #40773

Open
opened 2026-04-23 01:36:07 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14891
Author: @yjhjstz
Created: 3/17/2026
Status: 🔄 Open

Base: mainHead: feat/anthropic-num-ctx


📝 Commits (1)

  • 79f2527 anthropic: support num_ctx parameter in /v1/messages endpoint

📊 Changes

2 files changed (+70 additions, -0 deletions)

View changed files

📝 anthropic/anthropic.go (+5 -0)
📝 anthropic/anthropic_test.go (+65 -0)

📄 Description

Problem

The Anthropic-compatible /v1/messages endpoint ignores num_ctx, always defaulting to 4096 tokens regardless of the model's Modelfile configuration. This causes silent prompt truncation when system prompts and tool definitions exceed 4096 tokens.

Example log showing the problem:

level=WARN source=runner.go:186 msg="truncating input prompt" limit=4096 prompt=42218 keep=4 new=4096

The native /api/chat endpoint correctly handles num_ctx via request.Options, but the Anthropic endpoint has no equivalent mechanism.

Solution

Add a num_ctx field to MessagesRequest and forward it to the internal ChatRequest options, consistent with how the native endpoint works.

{
  "model": "my-model",
  "max_tokens": 1024,
  "num_ctx": 131072,
  "messages": [{"role": "user", "content": "hello"}]
}

Changes

  • anthropic/anthropic.go: Add NumCtx int field to MessagesRequest struct
  • anthropic/anthropic.go: Forward num_ctx to options in FromMessagesRequest() when set

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/14891 **Author:** [@yjhjstz](https://github.com/yjhjstz) **Created:** 3/17/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/anthropic-num-ctx` --- ### 📝 Commits (1) - [`79f2527`](https://github.com/ollama/ollama/commit/79f252746a5cc569717e70a8b0923544780255e7) anthropic: support num_ctx parameter in /v1/messages endpoint ### 📊 Changes **2 files changed** (+70 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `anthropic/anthropic.go` (+5 -0) 📝 `anthropic/anthropic_test.go` (+65 -0) </details> ### 📄 Description ## Problem The Anthropic-compatible `/v1/messages` endpoint ignores `num_ctx`, always defaulting to 4096 tokens regardless of the model's Modelfile configuration. This causes silent prompt truncation when system prompts and tool definitions exceed 4096 tokens. Example log showing the problem: ``` level=WARN source=runner.go:186 msg="truncating input prompt" limit=4096 prompt=42218 keep=4 new=4096 ``` The native `/api/chat` endpoint correctly handles `num_ctx` via `request.Options`, but the Anthropic endpoint has no equivalent mechanism. ## Solution Add a `num_ctx` field to `MessagesRequest` and forward it to the internal `ChatRequest` options, consistent with how the native endpoint works. ```json { "model": "my-model", "max_tokens": 1024, "num_ctx": 131072, "messages": [{"role": "user", "content": "hello"}] } ``` ## Changes - `anthropic/anthropic.go`: Add `NumCtx int` field to `MessagesRequest` struct - `anthropic/anthropic.go`: Forward `num_ctx` to options in `FromMessagesRequest()` when set --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-23 01:36:07 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#40773