Memory Tokens: Large Language Models Can Generate Reversible Sentence Embeddings (2025)

Abstract: In this work, we observe an interesting phenomenon: it is possible to generate reversible sentence embeddings that allow an LLM to reconstruct the original text exactly, without modifying the model’s weights. This is achieved by introducing a special memory token, whose embedding is optimized through training on a fixed sequence. When prompted with this embedding, the model reconstructs the fixed sequence exactly. We evaluate this phenomenon across English and Spanish datasets, sequences of up to approximately 240 tokens, and model scales ranging from 100M to 8B parameters. Notably, Llama 3.1 8B successfully reconstructs all tested sequences. Our findings highlight an interesting capability of LLMs and suggest potential applications in memory-based retrieval, compression, and controlled text generation.

## introduction large language models (llms) encode textual information in high-dimensional embeddings, capturing semantic and syntactic structures. in this work, we observe and explore an interesting phenomenon using llms: it is possible to construct reversible embeddings that encode an arbitrary text sequence in such a way that the original text can be perfectly reconstructed when used as input to an llm, without modifying the model's weights. this reversibility emerges when training a dedicated embedding associated with a special token, which we call a memory token, on a fixed sequence. by overfitting this embedding to a given text while keeping the model frozen, we show that the same model can autoregressively reconstruct the text when prompted with the learned embedding. we study this phenomenon, evaluating its effectiveness across different domains, sequence lengths, and languages (english and spanish). additionally, we explore models of various sizes, including gpt-2 @xcite and the llama 3 family @xcite . this observation sheds light on the representational capacity of llms and opens up new possibilities for memory-based retrieval and controlled text generation. it also suggests potential applications in text compression, adversarial attacks, and interpretability research. ## related work the method of optimizing a set of vectors and using them as a prefix for a specific task belongs to a class of techniques known as p*-tuning @xcite . some of these techniques are mentioned below. prefix tuning @xcite applies this idea as a lightweight alternative to full fine-tuning. it involves prepending a sequence of task-specific vectors to the input, optimizing these vectors while keeping the model frozen. building on prefix tuning, prompt-tuning @xcite applies the same principles and demonstrates the importance of scale: larger models get better results with this method and even become competitive with full fine-tuning. in a similar way, p-tuning @xcite is proposed as a method to improve the performance of discrete prompting. it employs trainable continuous prompt embeddings in concatenation with discrete prompts. more broadly, p*-tuning methods can be seen as a type of soft prompting in the context of prompt compression @xcite . soft prompt methods aim to compress text into a smaller number of special tokens. in this context, similar to our approach, @xcite propose training these embeddings using contrastive conditioning, without modifying the model's weights. while they focus on prompt compression, we aim to demonstrate the ability of llms to exactly reconstruct the original sequence. moreover, our work differs in how we train the embedding: we essentially overfit it to a single sequence. a more recent work in this line is 500xcompressor @xcite . it uses an encoder-decoder setup with lora parameters in the encoder and trains to encode textual information into the keyvalue (kv) pairs of compressed tokens. 500xcompressor is designed for prompt compression to improve llm efficiency in downstream tasks such as question answering, using a two-step training pipeline (pretraining and fine-tuning). in contrast, our work uses a decoder-only llm and directly optimizes a single embedding vector, without relying on an encoder. there has also been work on creating reversible sentence embeddings. @xcite propose a method for reconstructing text from contextualized embeddings generated by bert @xcite . their approach involves training a decoder model to recover the original text given the contextualized embedding as input. @xcite follow a similar approach to the one proposed in this work. they use already existing sentence embedding models to generate an embedding, which is then used as the initial input vector for a decoder model. the decoder is fine-tuned to reconstruct the original sequence. however, our approach differs in that we do not fine-tune the llm or rely on pre-trained sentence embedding models. ## memory token we define a memory token as a new token added to the model's vocabulary. this token has a corresponding embedding in the llm's embedding layer, that serves as a dense vector representation of an arbitrary text sequence. these embeddings are reversible, meaning that the original text sequence can be reconstructed from them, allowing the memory token to effectively store and retrieve the sequence it encodes. ## experiments to demonstrate this phenomenon and evaluate its scope and generalization, we construct datasets with varying characteristics, including different domains, sequence lengths, and languages. we then measure the ability of different models to reconstruct these sequences effectively. we evaluate the effectiveness of reconstruction using accuracy, which we define as the proportion of correctly predicted tokens with respect to the original sequence x. for each predicted token xt , the correct prefix @xmath0. the accuracy for a given sequence is then computed as: ## conclusions and future work we have presented a method for obtaining sentence embeddings from arbitrary text sequences using llms and demonstrated that, with models of at least 8b parameters, the original text can be reconstructed using the same llm, without modifying its weights. we observed that llama 3.1 8b was capable of perfectly reconstructing all the sequences. however, smaller models were less robust and had greater difficulty reconstructing longer sequences, suggesting that, as in many other tasks, model scale plays an important role in performance. this phenomenon suggests numerous potential applications and directions for future research. one potential application is in retrieval-augmented generation (rag) @xcite . memory tokens could be trained for each chunk. after the most relevant chunks are obtained in the retrieval step, instead of appending the full text of the retrieved chunks to the prompt, their corresponding embeddings could be used, significantly reducing the number of tokens required in the prompt. however, further work is needed to find a way to use these embeddings within the llm for purposes beyond merely reconstructing the original text. one possible direction is fine-tuning the model with examples where queries are answered using the correct memory tokens, allowing it to learn how to utilize them effectively. another important direction for future work is evaluating the effectiveness of the generated sentence embeddings in various downstream tasks, such as classification and retrieval, and comparing their performance with existing methods. this work also opens the door to exploring llms for compression and decompression of information, as memory tokens effectively store entire sequences in compact representations. additionally, our experiment with hurtful tweets shows that these embeddings can incite the models to generate harmful content, raising concerns about their potential use in adversarial attacks. finally, studying the mechanistic interpretability of llms when processing these embeddings could provide deeper insights into why this phenomenon occurs, ultimately contributing to a better understanding of how these models internally represent and retrieve information.