[Feature] Return actual prompt token IDs in opt-in Anthropic non-stream responses

Open
#5,234 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
1/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
api, backend

Research direction

Implementation PR #5232 is linked and already provides the requested behavior. Review that PR and the named regression tests, test_messages_non_stream_with_output_ids_and_routed_experts and test_messages_non_stream_does_not_add_prompt_ids_without_opt_in, to see how the response includes prompt IDs only when requested. Done means the PR is merged or its changes are otherwise present.

Written by the indexing model from the issue text.

Description

Motivation

Please expose the actual inference prompt token IDs in Anthropic non-streaming /v1/messages responses when the existing LMDeploy extension return_token_ids=true is requested.

The endpoint currently exposes output_ids under this flag, but does not include prompt_token_ids. For agentic RL trajectory recording, we need both the actual prompt IDs and generated IDs to align the recorded actions with the backend sequence. Re-tokenizing rendered messages is not a reliable substitute, especially with tool messages, chat templates, and image expansion.

This is a request for an opt-in LMDeploy extension, not a requirement of the standard Anthropic API.

Reproduction and expected behavior

Against an LMDeploy server with a supported model loaded:

curl http://localhost:23333/v1/messages \
  -H 'Content-Type: application/json' \
  -H 'anthropic-version: 2023-06-01' \
  -d '{
    "model": "<served-model-name>",
    "max_tokens": 16,
    "stream": false,
    "messages": [{"role": "user", "content": "Hi"}],
    "return_token_ids": true
  }'

Actual: the response includes output_ids, but omits prompt_token_ids.

Expected: also include prompt_token_ids from chat_runner.prompt_token_ids, preserving the backend's actual prompt sequence. When the flag is omitted or false, keep the current response shape and omit this extra field.

Related resources

Implementation PR: #5232, head b828647add4711a703b2d1a9a9c96dce79e140a3.

The PR reuses IDs already available on the chat runner; it adds a response field and conditional serialization rather than introducing another tokenizer path. Endpoint regression tests cover opt-in prompt IDs and default omission:

  • test_messages_non_stream_with_output_ids_and_routed_experts
  • test_messages_non_stream_does_not_add_prompt_ids_without_opt_in

Scope is non-streaming responses. Our current OpenAI-compatible rollout path is unaffected; this request is for the Anthropic trajectory-recording path.

Dominant language
Python
Stars
8.2k
Forks
797
Avg merge
3d 15h
Merged PRs (30d)
52

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from InternLM/lmdeploy

All issues in InternLM/lmdeploy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.