async def on_llm_new_token(self, token: str, **kwargs: dict[str, Any]) -> None:
        """Run on new LLM token. Only available when streaming is enabled."""
        if not self.streaming:
            self.streaming = True

        # Remember the last n tokens, where n = len(answer_prefix_tokens)
        self.append_to_last_tokens(token)

        # Check if the last n tokens match the answer_prefix_tokens list ...
        if self.check_if_answer_reached():
            self.answer_reached = True
            if self.stream_prefix:
                message = self._construct_message(
                    data=get_token_data("".join(self.last_tokens), self.mode),
                    event=Events.COMPLETION,
                )
                await self.send(message)

        # ... if yes, then print tokens from now on
        if self.answer_reached:
            message = self._construct_message(
                data=get_token_data(token, self.mode), event=Events.COMPLETION
            )
            await self.send(message)
Run on new LLM token. Only available when streaming is enabled.
lanarky/adapters/langchain/callbacks.py