How can I use custom tokens when grouping Thank You letters
The error comes from {capture assign='note_id'}{contribution.contribution_id}{/capture}. When grouping, if you have more than one contribution the output will be ie. 1, 2, 3, 4 ..., try printing {$note_id} when grouping and you'll see what I mean, passing 1, 2, 3, 4 as contribution_id to the API call the response is "is_error": 1

Edit: so having a look at CRM/Contribute/Form/Task/PDFLetterCommon.php, I believe that's the correct class, there´s a $contributions variable which holds the contributions, so if you loop through it, the above could look like this:

{foreach from=$contributions item=contribution}
{crmAPI var='result' entity='Note' action='get' entity_table="civicrm_contribution" entity_id=$contribution.id}
 {assign var='note_count' value=$result.count}
  Contribution note: {if !$note_count}None
  {elseif $note_count}
    {foreach from=$result.values item=note}
      {$note.note}
    {/foreach}
  {/if} 
 | Non deductible amount: {if $contribution.non_deductible_amount gt 0}${$contribution.non_deductible_amount}{else}N/A{/if}
&lt;br/&gt;
{/foreach}


Now this works when grouping but it won't when you are not grouping, so my guess is that you'll have to create two ThankYou templates, one for grouping and one for no grouping.

Also important to mention this is smarty they are not custom tokens.