Add Response.thoughts()
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 12.5k
- Forks
- 998
- Avg merge
- 3d 13h
- Merged PRs (30d)
- 10
Description
I see that you have an open issue for visible thinking tokens (#770), and that you had to abandon your first attempt. In the meantime, would it be possible to add a thoughts() or thinking() method to the Response class that returns the thinking tokens?
I'm aware that I can dig the thinking tokens out of the json response, but I'd like a less brittle, model-agnostic way of doing it. (In case it's not clear, I'm using llm as a python library rather than as a cli tool, and I'm trying to get at Anthropic's thinking tokens.)
Maybe this is too crude, but one possibility would be to add self.thoughts = None to _BaseResponse, and dig it out of the json object in ClaudeMessages and AsyncClaudeMessages:
class ClaudeMessages(_Shared, llm.KeyModel):
def execute(self, prompt, stream, response, conversation, key):
(...)
if stream:
(...)
# This records usage and other data:
response.response_json = stream.get_final_message().model_dump()
if <thinking>:
response.thoughts = response.response_json[(...)]
else:
(...)
yield prefill_text + text
response.response_json = completion.model_dump()
if <thinking>:
response.thoughts = response.response_json[(...)]
self.set_usage(response)
class AsyncClaudeMessages(_Shared, llm.AsyncKeyModel):
async def execute(self, prompt, stream, response, conversation, key):
(..)
if stream:
(...)
response.response_json = (await stream_obj.get_final_message()).model_dump()
if <thinking>:
response.thoughts = response.response_json[(...)]
else:
(...)
yield prefill_text + text
response.response_json = completion.model_dump()
if <thinking>:
response.thoughts = response.response_json[(...)]
self.set_usage(response)
I can put up a PR if you think this is worth pursuing.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the Response class and _BaseResponse, then trace ClaudeMessages and AsyncClaudeMessages to see how response_json is populated for streaming and non-streaming completions. Define how a model-agnostic thoughts() or thinking() method should expose Anthropic thinking tokens in both paths, and verify that existing response behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- ai
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100