Token Replacement via Lazy_builder
This answer might be helpful. In your case you could try to bubble up the attachment from the token itself:
$replacements[$original] = '&lt;my-placeholder&gt;';

$bubbleable_metadata-&gt;addAttachments([
  'placeholders' =&gt; [
    '&lt;my-placeholder&gt;' =&gt; [
      '#lazy_builder' =&gt; ['\Drupal\mytheme\MythemeLazyBuilders::replaceUid', []],
    ],
  ],
]);

You not really need the Drupal token system, you can attach placeholders almost everywhere in the rendering process, but for some cases this could be a convenient way to make a lazy builder available.